1

I need to match a pattern with strictly 10-symbols length OR 12 (not 11). So this won't work

[0-9]{10,12}

Can I write something more simple than

([0-9]{10}|[0-9]{12})

?

1
  • 1
    Please specify the language/regex flavour you use. Different flavours make different things possible. Commented Feb 18, 2014 at 16:34

2 Answers 2

3

You can use ? to set a char or group as optional :

\d{10}(\d\d)?

Don't forget to match start and end if that's the whole regex :

^\d{10}(\d\d)?$
Sign up to request clarification or add additional context in comments.

2 Comments

This is correct for this regex, but the pattern can be more complicated than just numbers
Then use the more complicated pattern, the important point is the ? to make an optionnal group.
1

Something like this may work..

(pattern)((\1){11}|((\1){9}))

I've just used backreference in php (\1)....

Comments

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.