1

I tried to make regex to validate phone number in format +38(0XX)XXX-XX-XX or 0XX-XXX-XX-XX. My regex is: '^(\+38)*(\(*0\d{2}\)*)[-|\s](\d{3})[-|\s]((\d{2})-|\s)+$'. And it's not matched any way. I reread re syntax a couple of times, can't get how to make it right.

1
  • It seems you meant +38(0XX)-XXX-XX-XX by looking at your regex. Commented Oct 23, 2011 at 8:05

2 Answers 2

1

UPDATE: Making the regex to match strictly as per the comments.

This regex should work according to your requirements

^((\+38)?\(0\d{2}\)|0\d{2})[-\s]\d{3}([-\s]\d{2}){2}$

This matches +38(022)-333-33-44, (022)-333-33-44 and 022-333-33-44

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

4 Comments

this will also match 022-333-33-44-77-33-44-23-56 and 045 333 44-37 :)
@scriptmonster OP had + in the end, I didn't want to change his regex completely. If needed he can replace + in the end with {2} or {3} as required. Also looks like OP's regex had both - and \s as possible delimiters.
Well but it will match with +38(022-333-33-44, +38022)-333-33-44 and +38022-333-33-44. I don't think that he's aware of that.
@scriptmonster I updated the regex to be more strict. Hope you won't rip it off any more ;)
1

This is an another option. It looks for exact formats that you mentioned.

'^(\+38\(0\d{2}\)\d{3}(-\d{2}){2})|(0\d{2}-\d{3}(-\d{2}){2})$'

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.