简体   繁体   中英

Using regular expressions to identify nodelists

The following information should be splitted in two arrays:

  • foo089,bar[120-123,215]

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:

  • foo[133,370,390-391],bar120
  • foo123
  • bar145
  • bar[121,303]
  • foo123,bar145

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

  • foo or bar don't have to exist
  • if both exist, they are always ordered
  • the numbers always have 3 digits
  • there are only foo and bar, no other words
  • foo234,foo425 is NOT allowed. If foo or bar has more than one number, it is listed in brackets

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:

  1. (?:\\d{3}(?:-\\d{3})? - three digits or three digits , dash , three digits
  2. (?:\\d{3}(?:-\\d{3})?,?)+ - the above repeated multiple times, possibly separated by a comma
  3. \\[(?:\\d{3}(?:-\\d{3})?,?)+\\] - same things but in square brackets
  4. |\\d{3} - alternatively, just three digits
  5. (?:foo(\\[(?:\\d{3}(?:-\\d{3})?,?)+\\]|\\d{3}))? the above, but preceded by foo and made optional with ?
  6. ,? - optional comma to separate foo and bar
  7. The bar part is pretty much the same as one for foo
  8. (?!foo\\d{3},bar\\d{3}) - disallow things like foo123,bar456
  9. ^$ - match from start to end

See it in action here

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.

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