你可以使用正则表达式来检查多个字符串是否出现在电话号码的开头。

huangapple go评论66阅读模式
英文:

How can I use Regex to check if multiple strings appear at the start of a phone number

问题

我试图检查电话号码是否以以下之一开头:031、039、076、077、078。我可以检查它们中的一个是否出现一次(096){1},但无法检查输入是否至少出现其中之一。我该如何检查这些序列是否出现?

英文:

I'm trying to check if a phone number has either of the following 031, 039, 076, 077, 078 at the start. I'm able to check if one of them occurs once
(096){1} but not able to check the input if any of them occur at least once.
How would I check if any of the sequences appear ?

答案1

得分: 1

^(?:031|039|076|077|078)\d+$

英文:

You may use an alternation here. Assuming the phone numbers were all digits, and you had no other requirements, you could use:

^(?:031|039|076|077|078)\d+$

答案2

得分: 0

^^(?:03[19]|07[678])\d+$$

Regex演示

请注意,这将从4位数字开始匹配。

如果您想要匹配至少10位数字,您可以使用一个量词:

^^(?:03[19]|07[678])\d{7,}$$

英文:

You can write the pattern with an alternation and character classes as:

^(?:03[19]|07[678])\d+$

Regex demo

Note that this will match starting from 4 digits.

If you want for example to match at least 10 digits, you can use a quantifier:

^(?:03[19]|07[678])\d{7,}$

huangapple
  • 本文由 发表于 2023年2月27日 12:16:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定