英文:
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+$$
请注意,这将从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+$
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,}$
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论