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).
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).
I always need two arrays, to easily determine whether the number belongs to foo or bar.
Restrictions
I tried the following regex so far: \\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.
^(?!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
.
Explanation:
(?:\\d{3}(?:-\\d{3})?
- three digits or three digits , dash , three digits (?:\\d{3}(?:-\\d{3})?,?)+
- the above repeated multiple times, possibly separated by a comma \\[(?:\\d{3}(?:-\\d{3})?,?)+\\]
- same things but in square brackets |\\d{3}
- alternatively, just three digits (?:foo(\\[(?:\\d{3}(?:-\\d{3})?,?)+\\]|\\d{3}))?
the above, but preceded by foo
and made optional with ?
,?
- optional comma to separate foo and bar (?!foo\\d{3},bar\\d{3})
- disallow things like foo123,bar456 ^$
- match from start to end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.