3

how do i return after the first match of regular expression? (does the Matcher.find() method do that? )

say I have a string "abcdefgeee". I want to ask the regex engine stop finding immediately after it finds the first match of "e" for example. I am writing a method to return true/false if the pattern is found and i don't want to find the whole string for "e". (I am looking for a regex solution )

Another question, sometimes when i use matches() , it doesn't return correctly. For example, if i compile my pattern like "[a-z]". and then use matches(), it doesn't match. But when I compile the pattern as ".*[a-z].*", it matches.... is that the behaviour of the matches() method of Matcher class?

Edit, here's actually what i want to do. For example I want to search for a $ sign AND a @ sign in a string. So i would define 2 compiled patterns (since i can't find any logical AND for regex as I know the basics).

pattern1 = Pattern.compiled("$");
pattern2 = Pattern.compiled("@");

then i would just use

if ( match1.find() && match2.find()  ){
  return true;
}

in my method.

I only want the matchers to search the string for first occurrence and return.

thanks

2
  • Your first question is confusing you say you want to return true/false but then you say you want to stop searching after you find a match? A regex is just going to match or not. Commented Jan 9, 2011 at 5:00
  • sorry, i have updated a bit on the question. Commented Jan 9, 2011 at 5:05

2 Answers 2

2

For your second question, matches does work correctly, you example uses two different regular expressions.

.*[a-z].* will match a String that has at least one character. [a-z] will only match a one character String that is lower case a-z. I think you might mean to use something like [a-z]+

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

2 Comments

if I have [a-z] as the compiled pattern, doesn't that match "2a" for example? Take for example i want to search just "e", then my compiled pattern should be "e", and not "e+" , since I only want to search for one "e". hmmm...am i wrong to say that ? sorry, am novice in regex
If you have pattern "[a-z]", it would only match "a", and omit "2". But still, if your string contains any chars from a to z, Matcher.find() will return true.
1

Another question, sometimes when i use matches() , it doesn't return correctly. For example, if i compile my pattern like "[a-z]". and then use matches(), it doesn't match. But when I compile the pattern as ".[a-z].", it matches.... is that the behaviour of the matches() method of Matcher class?

Yes, matches(...) tests the entire target string.

... here's actually what i want to do. For example I want to search for a $ sign AND a @ sign in a string. So i would define 2 compiled patterns (since i can't find any logical AND for regex as I know the basics).

I know you said you wanted to use regex, but all your examples seems to suggest you have no need for them: those are all singe characters that can be handled with a couple of indexOf(...) calls.

Anyway, using regex, you could do it like this:

public static boolean containsAll(String text, String... patterns) {
    for(String p : patterns) {
        Matcher m = Pattern.compile(p).matcher(text);
        if(!m.find()) return false;
    }
    return true;
}

But, again: indexOf(...) would do the trick as well:

public static boolean containsAll(String text, String... subStrings) {
    for(String s : subStrings) {
        if(text.indexOf(s) < 0) return false;
    }
    return true;
}

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.