I have a string that looks like:
ABC-DEF01-GHI54677-JKL!9988-MNOP
Between each - can be virtually any character repeated any number of times.
I’m using this regular expression:
[^-]*
How do I make it ‘match’ the match at the 2nd index (e.g. DEF01)? Or the 3rd (GHI54677) or 4th (JKL!9988)?
The engine I’m using doesn’t let me specify a match index or additional code – it has to all be done within the expression.
The second set of parens will capture “DEF”, “GHI”, and “JKL”, respectively…
If this is perl, make the first set of parens non-capturing, i.e.:
Explanation:
This part “gobbles up” 1, 2, 3, or any number you like, of the blocks, leaving the next set to take the one you’re looking for.
In lieu of
+, you can also use{1,}meaning 1-to-any-number.If your blocks can be zero size, so:
ABC–GHI-JKL
And you want to find the second, which is “” (empty string), then use
*instead of+. Or you can use{0,}, meaning 0-to-any-number.