Page 1 of 1
Regular Expression question for the advanced
Posted: Wed Jan 06, 2010 8:09 am
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
Re: Regular Expression question for the advanced
Posted: Wed Jan 06, 2010 6:49 pm
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!
