[英]Using regular expressions to identify nodelists
The following information should be splitted in two arrays: 以下信息应分为两个数组:
The desired result is two arrays with all the listed numbers in it [089] and [120-123,215] (even better would be 120,121,122,123 instead of 120-123, this is optional). 期望的结果是两个数组,其中包含所有列出的数字[089]和[120-123,215](甚至更好的是120,121,122,123而不是120-123,这是可选的)。
Other examples which should be parsed: 其他应解析的示例:
These all should result in a two-elemented array (first is foo, second is bar), with either a single entry (eg 123 or 145) or ranges (eg 133,370,390-391 or 121,303). 所有这些都将导致一个由两个元素组成的数组(第一个是foo,第二个是bar),具有单个条目(例如123或145)或范围(例如133,370,390-391或121,303)。
I always need two arrays, to easily determine whether the number belongs to foo or bar. 我始终需要两个数组,以轻松确定该数字属于foo还是bar。
Restrictions 限制
I tried the following regex so far: \\d{3}|foo\\d{3}|bar\\d{3}|\\d{3}-\\d{3}
. 到目前为止,我尝试了以下正则表达式:
\\d{3}|foo\\d{3}|bar\\d{3}|\\d{3}-\\d{3}
。 However, with preg_grep in php I don't get the desired result. 但是,在php中使用preg_grep时,我没有得到想要的结果。
^(?!foo\d{3},bar\d{3})(?:foo(\[(?:\d{3}(?:-\d{3})?,?)+\]|\d{3}))?,?(?:bar(\[(?:\d{3}(?:-\d{3})?,?)+\]|\d{3}))?$
Now in the match result, the first group is the numbers for foo
and the second group is the numbers for bar
. 现在在匹配结果中,第一组是
foo
的数字,第二组是bar
的数字。
Explanation: 说明:
(?:\\d{3}(?:-\\d{3})?
- three digits or three digits , dash , three digits (?:\\d{3}(?:-\\d{3})?
- 三位数或三位数 , 破折号 , 三位数 (?:\\d{3}(?:-\\d{3})?,?)+
- the above repeated multiple times, possibly separated by a comma (?:\\d{3}(?:-\\d{3})?,?)+
-以上重复多次,可能用逗号分隔 \\[(?:\\d{3}(?:-\\d{3})?,?)+\\]
- same things but in square brackets \\[(?:\\d{3}(?:-\\d{3})?,?)+\\]
-相同,但放在方括号中 |\\d{3}
- alternatively, just three digits |\\d{3}
-或者,仅三位数 (?:foo(\\[(?:\\d{3}(?:-\\d{3})?,?)+\\]|\\d{3}))?
the above, but preceded by foo
and made optional with ?
foo
之前加上?
,?
- optional comma to separate foo and bar (?!foo\\d{3},bar\\d{3})
- disallow things like foo123,bar456 (?!foo\\d{3},bar\\d{3})
-禁止使用诸如foo123,bar456之类的东西 ^$
- match from start to end ^$
-从头到尾匹配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.