简体   繁体   English

特定模式的正则表达式

[英]Regular expression for specific pattern

Could anybody help me with regular expression as mentioned. 有人可以帮助我进行正则表达式吗?

I have an xml element 'docs' which has an attribute 'range' where range should take only values in the below format. 我有一个xml元素“ docs”,其属性为“ range”,其中range应该仅采用以下格式的值。

range = "1,2,3,4,5" 范围=“ 1,2,3,4,5”

or 要么

range = "1,2,3-6,10-12,15,20-30" 范围=“ 1,2,3-6,10-12,15,20-30”

or 要么

range = "all" 范围=“全部”

A littel help will be much appreciated. 小小帮助将不胜感激。 Thanks in advance. 提前致谢。

^\d(?!.*,-)[\d\,\-]*\d$|^all$|^\d+$

Should work for your numbers. 应该适合您的电话号码。 Input can only be numbers, commas, or dash or the word all . 输入只能是数字,逗号或破折号或单词all

http://regexr.com?30p7f http://regexr.com?30p7f

all|[1-9][0-9]*(-[1-9][0-9]*)?(,[1-9][0-9]*(-[1-9][0-9]*)?)*

I have assumed that zero and leading zeroes are not permitted. 我假设零和前导零是不允许的。

Regular expressions are not a good way to ensure that numbers are in ascending order. 正则表达式不是确保数字升序的好方法。

As @minopret mentioned, this is probably best not handled completely with regex. 正如@minopret所提到的,最好不要使用正则表达式完全解决此问题。 I would split(",") the strings and then iterate over the resulting array to make sure that they are in ascending order. 我将split(",")字符串,然后遍历结果数组以确保它们按升序排列。 You can regex each of the individual array elements at that point to make sure they match something like: 您可以在此时对每个单独的数组元素进行正则表达式,以确保它们与以下内容匹配:

\d+(-\d+)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM