如何仅在正则表达式中允许WhatsApp格式的数字? [英] How to allow only WhatsApp format numbers in a regex?

查看:116
本文介绍了如何仅在正则表达式中允许WhatsApp格式的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正试图使此正则表达式允许它成为 Dash符号-例如,此电话号码目前不匹配

+212 659-123456

所以我需要有人 帮助,我更改正则表达式以允许它:这是正则表达式:

^ \ +(?:[0-9] \ x20?){6,14} [0-9] $

因为我试图仅接受WhatsApp使用的格式,并且某些数字可能具有多个空格或多个短划线.另外,加号也必须是强制性的,此处提供了该格式的更多示例在华盛顿州.

+96274567123

+ 967773-123-123

+212 627-024321

+ 212689-881234

+966 54 666 4373

以上数字涵盖了99%的案件.我将不胜感激,谢谢和问候

解决方案

我只会使用:

  ^(?=(?:[+-] * [0-9] [+-] *){11,12} $)\ +(?:[0-9] + [-]?)+ [0-9] $ 

说明:

  • (?=(?:[+-] * [0-9] [+-] *){11,12} $)正向检查该字符串是否具有恰好是11或12位数字.

  • \ +(?:[0-9] + [-]?)+ [0-9] 必须以 + 开头并结束带有一个数字的字符,可以在一个或多个数字之间加上一个可选的单个 -组成的组.

so I'm trying to make this Regex allow this the Dash symbol - For Example this Phone Number is not matching right now

+212 659-123456

So I need someone to help me change the Regex to allow it please Here is the Regex:

^\+(?:[0-9]\x20?){6,14}[0-9]$

Because I am trying to only accept the format that is used by WhatsApp and some numbers might have multiple spaces or multiple Dashes. Also the Plus sign has to be mandatory Here some more examples of the format on WA.

+96274567123

+967773-123-123

+212 627-024321

+212689-881234

+966 54 666 4373

The numbers above cover 99% of the cases. I would appreciate any help, thanks and regards

解决方案

I would just use:

^(?=(?:[+ -]*[0-9][+ -]*){11,12}$)\+(?:[0-9]+[ -]?)+[0-9]$

Explanation:

  • (?=(?:[+ -]*[0-9][+ -]*){11,12}$) Positive lookahead which checks that the string has exactly 11 or 12 digits in it.

  • \+(?:[0-9]+[ -]?)+[0-9] Has to start with a + and end with a digit, in between can be groups of one ore more digits plus optionally a single or -.

regex101 demo

这篇关于如何仅在正则表达式中允许WhatsApp格式的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆