正则表达式匹配可选的“+"符号后跟任意数量的数字 [英] Regex to match an optional '+' symbol followed by any number of digits

查看:42
本文介绍了正则表达式匹配可选的“+"符号后跟任意数量的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个正则表达式来匹配一个字符串,该字符串可能以也可能不以加号开头,然后包含任意数量的数字.

I want a regular expression to match a string that may or may not start with plus symbol and then contain any number of digits.

那些应该匹配

  +35423452354554
  or
  3423564564

推荐答案

这应该可行

\+?\d+

匹配行首的可选 + 及其后的数字

Matches an optional + at the beginning of the line and digits after it

根据 OP 的澄清要求:3423kk55 匹配,因为它是第一部分(3423).要匹配整个字符串,请改为使用它:

As of OP's request of clarification: 3423kk55 is matched because so it is the first part (3423). To match a whole string only use this instead:

^\+?\d+$

这篇关于正则表达式匹配可选的“+"符号后跟任意数量的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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