PowerShell regex and e.164 telephone numbers -
i'm looking telephone numbers have 12 or 13 digits.
the following 2 regex work:
ps c:\data\umcp> $bla = [regex]'^(\+[3][9])?([0-9]\d{9})$' ps c:\data\umcp> $twelv -match $bla true ps c:\data\umcp> $bla = [regex]'^(\+[3][9])?([0-9]\d{10})$' ps c:\data\umcp> $thirt -match $bla true but
ps c:\data\umcp> $bla = [regex]'^(\+[3][9])?([0-9]\d{9} | ^\+[3][9])?([0-9]\d{10})$' ps c:\data\umcp> $thirt -match $bla true ps c:\data\umcp> $twelv -match $bla false how should use | or?
you have whitespaces around pipe, , whitespace matters. make whitespace (like this, e.g.) "formatting whitespace", add (?x) ignorepatternwhitespace inline modifier start of pattern.
also, should careful unescaped parentheses, make groups , must paired.
your fixed pattern like
'^(?:(?:\+[3][9])?[0-9]\d{9}|(?:\+[3][9])?[0-9]\d{10})$' | |--opt.gr.-| | |-opt.gr.--| | |---------- branch 1------|-------- branch 2-------| see how groups created: outer parentheses create group ^ , $ applied both alternative branches, , inner ones used form optional group (with ? quantifier after them).
if have string matching pattern:
- an optional
+39@ start - 10 or 11 digits end of string
use
^(?:\+39)?[0-9]{10,11}$ see regex demo
Comments
Post a Comment