简体   繁体   English

结合两个正则表达式的PHP

[英]Combine two regular expressions for php

I have these two regular expression 我有这两个正则表达式

^(((98)|(\+98)|(0098)|0)(9){1}[0-9]{9})+$
^(9){1}[0-9]{9}+$

How can I combine these phrases together? 如何将这些短语组合在一起?

valid phone : 有效电话:

just start with : 0098 , +98 , 98 , 09 and 9
sample :
00989151855454
+989151855454
989151855454
09151855454
9151855454

You haven't provided what passes and what doesn't, but I think this will work if I understand correctly... 您没有提供通过和不通过的内容,但是我认为,如果我理解正确的话,这将起作用。

/^\+?0{0,2}98?/

Live demo 现场演示

^         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

Looking at your second regex, it looks like you want to make the first part ((98)|(\\+98)|(0098)|0) in your first regex optional. 查看您的第二个正则表达式,您似乎想使第一个正则表达式的第一部分((98)|(\\+98)|(0098)|0)为可选。 Just make it optional by putting ? 只需通过放置使其成为可选? after it and it will allow the numbers allowed by second regex. 之后,它将允许第二个正则表达式允许的数字。 Change this, 改变这个

^(((98)|(\+98)|(0098)|0)(9){1}[0-9]{9})+$

to, 至,

^(?:98|\+98|0098|0)?9[0-9]{9}$
                   ^ this makes the non-grouping pattern optional which contains various alternations you want to allow.

I've made few more corrections in the regex. 我在正则表达式中做了一些更正。 Use of {1} is redundant as that's the default behavior of a character, with or without it. 使用{1}是多余的,因为这是字符的默认行为(有或没有)。 and you don't need to unnecessarily group regex unless you need the groups. 除非您需要分组,否则您不需要不必要地将正则表达式分组。 And I've removed the outer most parenthesis and + after it as that is not needed. 并且我删除了最外面的括号,并在其后加上了+ ,因为这是不必要的。

Demo 演示版

This regex 这个正则表达式

^(?:98|\+98|0098|0)?9[0-9]{9}$

matches 火柴

00989151855454
+989151855454
989151855454
09151855454
9151855454

Demo: https://regex101.com/r/VFc4pK/1/ 演示: https : //regex101.com/r/VFc4pK/1/

However note that you are requiring to have a 9 as first digit after the country code or 0 . 但是请注意,您要求在国家/地区代码后的第一个数字为90

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM