[英]Combine two regular expressions for php
我有這兩個正則表達式
^(((98)|(\+98)|(0098)|0)(9){1}[0-9]{9})+$
^(9){1}[0-9]{9}+$
如何將這些短語組合在一起?
有效電話:
just start with : 0098 , +98 , 98 , 09 and 9
sample :
00989151855454
+989151855454
989151855454
09151855454
9151855454
您沒有提供通過和不通過的內容,但是我認為,如果我理解正確的話,這將起作用。
/^\+?0{0,2}98?/
^ Matches the start of the string
\+? Matches 0 or 1 plus symbols (the backslash is to escape)
0{0,2} Matches between 0 and 2 (0, 1, and 2) of the 0 character
9 Matches a literal 9
8? Matches 0 or 1 of the literal 8 characters
查看您的第二個正則表達式,您似乎想使第一個正則表達式的第一部分((98)|(\\+98)|(0098)|0)
為可選。 只需通過放置使其成為可選?
之后,它將允許第二個正則表達式允許的數字。 改變這個
^(((98)|(\+98)|(0098)|0)(9){1}[0-9]{9})+$
至,
^(?:98|\+98|0098|0)?9[0-9]{9}$
^ this makes the non-grouping pattern optional which contains various alternations you want to allow.
我在正則表達式中做了一些更正。 使用{1}
是多余的,因為這是字符的默認行為(有或沒有)。 除非您需要分組,否則您不需要不必要地將正則表達式分組。 並且我刪除了最外面的括號,並在其后加上了+
,因為這是不必要的。
這個正則表達式
^(?:98|\+98|0098|0)?9[0-9]{9}$
火柴
00989151855454
+989151855454
989151855454
09151855454
9151855454
演示: https : //regex101.com/r/VFc4pK/1/
但是請注意,您要求在國家/地區代碼后的第一個數字為9
或0
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.