2

I have a specific question, to which I couldn't find any answer online. Basically, I would like to run a pattern-matching operation on a text, with multiple patterns. However, I do not wish that the matcher gets me the result all at once, but instead that each pattern is called at different stages of the loop, at the same time that specific operations are performed on each of these stages. So for instance, imagining I have Pattern1, Pattern2, and Pattern3, I would like something like:

 if (Pattern 1 = true) {
        delete Pattern1;
    } else if (Pattern 2 = true) {
        delete Pattern2;
    } else if (Pattern 3 = true) {
        replace with 'something;
    } .....and so on

(this is just an illustration of the loop, so probably the syntax is not correct, )

My question is then: how can I compile different patterns, while calling them separately? (I've only seen multiple patterns compiled together and searched together with the help of AND/OR and so on..that's not what I'm looking for unfortunately) Could I save the patterns in an array and call each of them on my loop?

2
  • 1
    not really sure i see the question. your bullet points above are practically the pseudo code. just throw in some if blocks... Commented Aug 29, 2012 at 16:23
  • I haven't decided which loop I will use, that's why it looks so abstract at the moment, but I'll give it a try. Commented Aug 29, 2012 at 16:29

2 Answers 2

2

Prepare your Pattern objects pattern1, pattern2, pattern3 and store them at any container (array or list). Then loop over this container using usePattern(Pattern newPattern) method of Matcher object at each iteration.

Sign up to request clarification or add additional context in comments.

3 Comments

and in case I need to compile the patterns (I'm sure I do), do I need to compile them one by one, or could this be done using the container? Thank you, by the way!
Whenever you want. In my previous comment 'Prepare' == 'Get compiled Pattern object'. In some of my projects I used list with "static" and context-depending patterns. So firstly I compiled "static" patterns and added them to list and then added dynamically formed patterns on demand.
And also I should mention boundary matcher \G ("The end of the previous match"). It's usefull when you sequentially use match() with different patterns.
1

You can make a common interface, and make anonymous implementations that use patterns or whatever else you may want to transform your strings:

interface StringProcessor {
    String process(String source);
}

StringProcessor[] processors = new StringProcessor[] {
    new StringProcessor() {
        private final Pattern p = Pattern.compile("[0-9]+");
        public String process(String source) {
            String res = source;
            if (p.matcher(source).find()) {
                res = ... // delete
            }
            return res;
        }
    }
,   new StringProcessor() {
        private final Pattern p = Pattern.compile("[a-z]+");
        public String process(String source) {
            String res = source;
            if (p.matcher(source).find()) {
                res = ... // replace
            }
            return res;
        }
    }
,   new StringProcessor() {
        private final Pattern p = Pattern.compile("[%^#@]{2,5}");
        public String process(String source) {
            String res = source;
            if (p.matcher(source).find()) {
                res = ... // do whatever else
            }
            return res;
        }
    }
};

String res = "My starting string 123 and more 456";
for (StringProcessor p : processors) {
    res = p.process(res);
}

Note that implementations of StringProcessor.process do not need to use regular expressions at all. The loop at the bottom has no idea the regexp is involved in obtaining the results.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.