Java regex: match expression delimited by whitespaces -
phone number like:
+38097-12-34-123 +380971234-123 380971234-123 i should match phone numbers in string assuming separated whitespaces. not matched strings:
45+38097-12-34-123 +38097-12-34-123=test number:38097-12-34-123 working regex phone number without whitespace delimiter requirements is:
\\+?380\\d\\d(?:-?\\d){7} i try tests \b / \b boundary matcher don't work if leading + sign used.
update fixed examples because type memory , forget put 8 sign.
update2 @washington guedes regex (i strip \ \-):
\b(\+?\d{4}(?:-\d{2}-|\d{2})\d{2}-\d{3})\b doesn't work because when + in matching string:
bash# groovysh groovy:000> "45 +8097-12-34-123".matches(".*\\b(\\+?\\d{4}(?:-\\d{2}-|\\d{2})\\d{2}-\\d{3})\\b.*") ===> true groovy:000> "45+8097-12-34-123".matches(".*\\b(\\+?\\d{4}(?:-\\d{2}-|\\d{2})\\d{2}-\\d{3})\\b.*") ===> true i expect expession without whitepsace before + sign should fail.
@stribizhev same suggestion:
groovy:000> "45+38097-12-34-123".matches(".*(?<!\\w)\\+?380\\d\\d(?:-?\\d){7}(?!\\w).*") ===> true groovy:000> "45 +38097-12-34-123".matches(".*(?<!\\w)\\+?380\\d\\d(?:-?\\d){7}(?!\\w).*") ===> true
the main difficulty have making word boundary work before optional subpattern \+?. trailing word boundary, can use either \b or (?!\w) (which preferable when context unknown, here, have obligatory digit, so, \b enough).
due backtracking, \+? "gobbled" , lookbehind (?<!\w) checks if first digit not preceded non-word character. since in 45+38097-12-34-123, 3 preceded non-word +, matched.
a solution can using alternative in lookahead:
string re = ".*\\+?(?<!\\w|\\w\\+)380\\d\\d(?:-?\\d){7}\\b.*"; string s = "45+38097-12-34-123"; system.out.println(s.matches(re)); see ideone demo
Comments
Post a Comment