Reputation: 5012
I tried to match only the 9 digits of phone number
I have multi input
+33123456789
or
0123456789
or
+33 01 23 45 67 89
or
01 23 45 67 89
I want one output 123456789
currently I have found how to match +33123456789 or 0123456789
with (\d{9}$)
But I don't find how to match with space between
thank you
Upvotes: 1
Views: 922
Reputation: 43169
You could use
(?:\+\d{2}\ ?)?(\d[\d ]*\d)
and use the first group, see a demo on regex101.com.
Upvotes: 1
Reputation: 163362
You could use repeat matching a digit followed by 0+ times a space 8 times. For the last digit you could also match 0+ times a space and a digit and capture that in a group.
After the captured group match 0* times a space and assert the end of the string.
((?:\d *){8} *\d) *$
Explanation
(
Capturing group
(?:\d *){8}
Repeat 8 times a non capturing group that matches a digit and 0+ times a space*\d
match 0+ times a space followed by a digit)
Close non capturing group*$
Match 0+ times a space and assert the end of the stringThe 9 digits including whitespace between them are in the first capturing group.
Upvotes: 1