Regular Expression question for the advanced

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
kevinkhan
Forum Newbie
Posts: 9
Joined: Wed Jan 06, 2010 8:08 am

Regular Expression question for the advanced

Post by kevinkhan »

I have a regular expression to match numbers in this format

087xxxxxxx
086xxxxxxx
085xxxxxxx
083xxxxxxx
(087) xxxxxxx
(087)xxxxxxx
( 087 )xxxxxxx
( 087 ) xxxxxxx
087 xxxxxxx
087-xxxxxxx
087 - xxxxxxx

Code: Select all

#([( ]{0,2}08[3567])[) -]{0,3}([0-9]{7})#
How do i write a regex to take numbers in the above formats but not numbers that have a digit repeated more than 4 times like this
0879811111

Thanks
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Regular Expression question for the advanced

Post by ridgerunner »

Its not pretty, but this one should do the trick:

Code: Select all

// Long version with comments:
'%
(?=     # start a positive assertion for "no five digits in a row" logic
  ^     # anchor to start of line/string
  (?:   # start non-capture group to walk the string one char at a time
    (?!00000|11111|22222|33333|44444|55555|66666|77777|88888|99999) # no 5 in a row?
    .   # Ok. at this position there are not four digits in a row - match this char
  )*$   # repeat, checking each and every char, one at a time up to end of line
)       # success. this string does not have five digits in a row
([( ]{0,2}08[3567])[) -]{0,3}([0-9]{7})  # now match your regex
%mx'
 
// short version
'/(?=^(?:(?!00000|11111|22222|33333|44444|55555|66666|77777|88888|99999).)*$)([( ]{0,2}08[3567])[) -]{0,3}([0-9]{7})/m'
This regex only works if the sequence is on a line by itself with no leading or trailing characters (i.e. it cannot be embedded in a longer text.)

Hope this helps! :)
Post Reply