Echilon
Echilon

Reputation: 10264

Regex Matching Phone Number

I'm trying to create a regex which will match anything which looks like a phone number. If there's more than one number in a string, match both of them. A phone number is defined as:

So I'd like to match these:

And disallow these:

I've toyed with negative lookahead/lookbehind but I can't get anything intelligible out. Is tis even possible or shall I do it in a higher language like .NET?

Upvotes: 0

Views: 147

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336448

(?:\d\s*){10,}(?![\d\s]*N)

will match a 10+ digit phone number within a longer string, as long as that number is not followed by N. It allows any number of spaces between each digit.

If all your phone numbers always start with 0 as in your example, you can explicitly code that into the regex:

\b0\s*(?:\d\s*){9,}(?![\d\s]*N)

See it on RegExr.

Upvotes: 1

Related Questions