how can i return after the first regex match? (does the Matcher.find () method do this?)
Say I have an abcdefgeee line. I want to ask that the regex engine stops right after it finds the first match โeโ, for example. I am writing a method that returns true / false if a pattern is found, and I do not want to find the whole string for "e". (I'm looking for a regex)
Another question, sometimes when I use match (), it does not return correctly. For example, if I compile my template as "[az]". and then use match (), this does not match. But when I compile the template like ".*[a-z].*", it matches .... is this the behavior of the match () method of the Matcher class?
Change, thatโs actually what I want to do. For example, I want to find the $ AND sign and the @ sign in a string. Therefore, I would define 2 compiled patterns (since I cannot find a logical AND for regular expression, since 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 want the matches to look for a string for the first appearance and return.
thank
source
share