普通防爆pression验证数字与一个空格和一个可选的特殊字符 [英] Regular Expression for validating numbers with one space and one optional special character

查看:151
本文介绍了普通防爆pression验证数字与一个空格和一个可选的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何验证数字序列与一个空格,并在年底会有一个可选的特殊字符说'#',再其次是一些5位。

Can someone tell me how to validate sequence of numbers with one space and at the end there will be a optional special character say '#' and again followed by some 5 digits.

例如:

12345 6587#2584

12345 6587#2584

我曾尝试与

(0(?:\d{0,11}|(?=\d* \d*$)[\d ]{0,12}))

但我不知道如何添加到底后面跟着数字可选的'#'。

but I don't know how to add optional '#' in the end followed by digits.

推荐答案

这应该做的伎俩

/^\d+\s\d+(?:#\d+)?$/

看到它在 rubular

^      beginning of string
\d+    one or more numbers
\s     any whitespace character
\d+    one or more numbers
(?:    begin non-capturing group
  #    literal '#' character
  \d+  one or more numbers
)      end non-capturing group
$      end of string


修改

/^0[\d\s]{,11}(?:#\d{,5}?$/

比赛开始 0 字符串,然后是一个最大的11个数字或空格。其次是一个可选的用最大的5个号码后。

Matches a string starting with 0, followed by a max of 11 numbers or spaces. Followed by an optional # with a max of 5 numbers after it.

这篇关于普通防爆pression验证数字与一个空格和一个可选的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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