0

So I am working on regex comparing phone numbers and this is the result:

(?:(?:0{2}|\+)?([1-9][0-9]))? ?([1-9][0-9])? ?([1-9][0-9]{5})

As you can see there are spaces between the numbers. I want them to appear only when there is some other number before the space so:

"0022 45 432345" - should match "45 345678" or "560032" - should match " 324400" - shouldn't match because of the space in the beginning

I've been reading different tutorials about regexes and found out about look-behinds, but simple construction like that(just for test):

Pattern p2 = Pattern.compile("(?<=abc)aa");
Matcher m2 = p2.matcher("abcaa");

doesn't work.

Can you tell me what's wrong?

Another problem is - I want a character only happen when it is THE FIRST character in a string, otherwise it shouldn't occur. So the code: 0043 022 234567 should not work, but 022 123450 should match.

I'm stuck right now and would appreciate any help a lot.

żyńy
  • 83
  • 1
  • 1
  • 6
  • you might want to look [here](https://stackoverflow.com/questions/2113908/what-regular-expression-will-match-valid-international-phone-numbers) – Ivonet Dec 11 '17 at 23:02
  • Please expand on what you mean that it "doesnt work". Reminder that the lookbehind part of your pattern is NOT a part of the actual match itself; ie the example should return `aa` and NOT `abcaa` – R Nar Dec 11 '17 at 23:08
  • `^\d+[\s\d]*$` should do what you need without the need of look-behind constructs. – Lothar Dec 11 '17 at 23:12

2 Answers2

0

Lookbehind is a zero length match.

The javadoc for the Matcher.matches method determines if the whole String is a match.

What you're looking for is something the Matcher.find and Matcher.group methods. Something like:

final Pattern pattern = Pattern.compile("(?<=abc)aa");
final Matcher matcher = pattern.matcher("abaca");

final String subMatch;
if (matcher.find()) {
    subMatch = matcher.group();
} else {
    subMatch = "";
}

System.out.println(subMatch);

Example.

BeUndead
  • 3,463
  • 2
  • 17
  • 21
0

This should work just fine. The spaces are moved into the optional groups and are themselves optional. This way, they only match if the group before them is present, but even then they are still optional. No look-behind required.

(?:(?:(?:00|\+)?([1-9][0-9]) ?)?([1-9][0-9]) ?)?([1-9][0-9]{5})
Leo Aso
  • 11,898
  • 3
  • 25
  • 46
  • Thank you, that works great. There is another problem that I encounter quite often. So the second group can have 0 in the beginning, so numbers like +44 034 543000 is NOT ok, but 034 543000 is totally ok. I need to put 0 in the second group so that zero occurs only if there is nothing before it. Can you point me at least in some direction what do I need to use? – żyńy Dec 12 '17 at 15:58
  • @żyńy then you can use `(?:(?:(?:00|\+)?([1-9][0-9]) ?|0)?([1-9][0-9]) ?)?([1-9][0-9]{5})` – Leo Aso Dec 12 '17 at 16:25
  • Thx, that could work, but somehow the restriction is this "0" needs to be in the second group. – żyńy Dec 12 '17 at 21:16
  • Ok, the problem solved adding (?:^0)? to the second group. Thx for help. – żyńy Dec 12 '17 at 21:18