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.
2 Answers
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
4 Comments
scriptmonster
this will also match 022-333-33-44-77-33-44-23-56 and 045 333 44-37 :)
Narendra Yadala
@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.scriptmonster
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.
Narendra Yadala
@scriptmonster I updated the regex to be more strict. Hope you won't rip it off any more ;)
+38(0XX)-XXX-XX-XXby looking at your regex.