regex - How do I choose a regular expression match at an arbitrary index? -


I have a string that looks:

ABC-DEF01-GII 54677- JKL! 9988-MNOP

Any character can be repeated repeatedly between each - .

I am using this regular expression: How can I match the match of the second match (eg DEF 01)? Or third (GII 54677) or 4 (JKL! 9988)?

The engine I am using does not allow me to specify a mail index or an additional code - it has to be done within the expression

< The second group of legs will occupy the "DEF", "GHI" and "JKL" respectively ...

  (

If this is perl, then non-capturing the first set of scales, that is:

# perl -de 0 $ _ = "ABC -def-gii -JKL-MNO "P / (?: [^ -] + -) {1} ([^ -] +) / DEF p / (?: [^ -] + -) {2} ([^ -] + ) / Ghi p / (?: [^ -] + -) {3} ([^ -] +) / JKL $ _ = "ABC-DEF01-GII 54677-JKL! 9988-MNOP" p / (?: [ ^ -] + -) {1} ([^ -] +) / DEF 01 p / (?: [^ -] + -) {2} (<^ -) +) / GHI54677 p / (?: [^ -] + -) {3} ([^ -] +) / JKL! 9988

explanation:

  (?: = Non-capturing parance [^ -] = a non-dash character + = one or more - = a Dash) = Close parent {3} = repeat 3 times  

This section blocks 1, 2, 3, or any N, which you like from the blocks, which you are looking for Except the next set to pick it up.

+ , you {1

ABC - GHI-JKL

P> and you want second, which is "" (empty string), then use + instead of * . Or you can mean any number from 0 to {0,} .


Comments