0

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

1 Answer 1

2

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

Not quite, it didn't work I just ended up with 12345678 it didn't include the (06) at the start.
Thanks for the refiddle, however if you change it from (06) to, 12345678 then it doesn't match. The () are optional. Hence the /(? on each one. Examples added to the question.
Nearly there, the last one shouldn't match, which is where the whole word boundary came into it (see OP). So =12345678 should not match

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.