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:
- 10+ characters
- Does not end in
N, but can end in other letters/words
So I’d like to match these:
- 07158245215
- 01244356356
- 07158245215Y
- 01244356356Y
- 07158245215P
- 01244356356P
- 07158245215X
- 01244356356X
- 07158245215 work
- 01244 356356 work
- work 07158 245215 / home 07158 245215 // might be a difficult one
- work 01244356356
And disallow these:
- 071582 45215N
- 01244356356N
- 01244356356 N
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?
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
0as in your example, you can explicitly code that into the regex:See it on RegExr.