Saphire
Saphire

Reputation: 1930

RegEx phone number doesn't match

I am trying to match following formats:

06142/898-301
+49 6142 898-301

with this

(([+][\d]{2}[ ])|0)([\d]{4}/)([/d]{2,}[.-])+

Regular expression visualization

Debuggex Demo

But after the area code before the / it won't match anymore. Why?

Upvotes: 0

Views: 56

Answers (2)

brandonscript
brandonscript

Reputation: 72965

Looks like you want something more like this:

^(\+\d{2} |0)\d{4}[/ ]\d{3}[.-]\d{3}$

Example: http://regex101.com/r/qG2zY2

You don't need character classes defined for a single character, and you probably don't need all the capture groups either. I also added the anchor characters in there (^, $) but you can remove them if you're trying to pick this out of a larger string.

Upvotes: 0

Kent
Kent

Reputation: 195169

you mean this?

(([+][\d]{2}[ ])|0)([\d]{4}/)([\d]{2,}[.-])+

what I changed in your expression:

[/d]{2,} - > [\d]{2,}  actually \d{2,} would do too

Upvotes: 1

Related Questions