Reputation: 1511
I have a Regex which extracts German mobile phone numbers from a website:
[^\d]((\+49|0049|0)1[567]\d{1,2}([ \-/]*\d){7})(?!\d)
As you can see in the demo it works quite well. The only pattern which doesn't match yet is:
+49 915175461907
Please see more examples in the linked demo. The problem is the whitespace behind +49
.
How do I need to change the current regex pattern in order to match even these kind of patterns?
Upvotes: 3
Views: 378
Reputation: 4749
Add an optional white space:
[^\d]((\+49|0049|0)\s?(1|9)[1567]\d{1,2}([ \-/]*\d){7,8})(?!\d)
Update-Capturing beginning of line
If you want is to match numbers without them necessarily starting with a line break you can use this. It matches anything except digits before phone number:
(^|[^\d])((\+49|0049|0)\s?(1|9)[1567]\d{1,2}([ \-/]*\d){7,8})(?!\d)
Upvotes: 1
Reputation: 785651
A better regex would be:
(?<!\d)(?:\+49|0049|0) *[19][1567]\d{1,2}(?:[ /-]*\d){7,8}(?!\d)
Changes:
(?<!\d)
: Make sure previous character is not a digit[19][1567]
: Match 1
or 9
followed by one of the [1567]
digit{7,8}
: Match 7 or 8 repetitions of given constructUpvotes: 5
Reputation: 79
No brain method : removing space before regex.
Otherwise matching non withe space in regex is \s so (maybe too much parenthesis)
[^\d](((\+49|0049|0)([\s]{0,1})1)[567]\d{1,2}([ \-/]*\d){7})(?!\d)
Upvotes: 3