Wizzard
Wizzard

Reputation: 12702

Regex with a word boundary in python

I am trying to captcha a particular string with a regex, however I am losing the first char.

The string is (06)12345678

My regex is

r'\b\((0[34679]{1})\)?([\- ]{0,1})[0-9]{3,4}([\- ]{0,1})[0-9]{3,5}'

but all I get in my match is

06)12345678

I really want that first ( also.

The ( and ) are conditional because sometimes there wont be (). but the word boundary are there to prevent numbers like

hello123456789 

matching

regex = r'\b\(?(0[34679]{1})\)?([\- ]{0,1})[0-9]{3,4}([\- ]{0,1})[0-9]{3,5}'
matches = re.finditer(regex, '(06)12345678)')
for match in matches:
    print match.group(0)

any thoughts?

-- examples --

(06)12345678 should match, (06)12345678
06 12345678 should match, 06 12345678
1234567890 should match, 1234567890
=12345678 no match

Upvotes: 0

Views: 442

Answers (1)

AbstractProblemFactory
AbstractProblemFactory

Reputation: 9811

Try escape second "(", not first one, and the last but one ")" before first ?.

Live demo: https://regex101.com/r/5h3FBr/1

Upvotes: 2

Related Questions