java - Regular expression wtih an escaped closing bracket at the end -
this java/regular expression question.
i search "m(x)" in text string in java program using following snippet:
public static list<string> findpattern(string patternstr, string value) { arraylist<string> matchedstrings = new arraylist<string>(5); pattern pattern = pattern.compile(patternstr); matcher matcher = pattern.matcher(value); while (matcher.find()) { matchedstrings.add(matcher.group(0)); } return matchedstrings; } the following pattern string works:
m\\\\(x\\\\) the following pattern doesn't:
\\\\bm\\\\(x\\\\)\\\\b i use \\b such can find match text "m(x) = abc" , not "tm(x) = abc". interesting, testing had done, "\\b" seems fail if character right before escaped bracket.
is there wrong? or there can achieve same objective?
thanks , regards
see, \b word boundary anchor. in other words, marks place right in between word characters (letters, digits, underscore) , non-word ones (the rest).
the point is, both ')' , ' ' (space character) non-word characters, there no word boundary between them. if expect in pattern, it'll fail.
so, can drop trailing \b pattern, turning into...
\\bm\\(x\\)
Comments
Post a Comment