I am trying to parse a pattern with regular expressions in Ruby. The pattern is something like,
<number>? <comma>? <number>? <term>*
where:
number
is one or more digitscomma
is","
term
is of the form[.*]
or[^.*]
And I am trying to capture the numbers, and all the terms. To clarify, here are some examples of valid patterns:
5,50[foo,bar]
5,[foo][^apples]
10,100[baseball][^basketball][^golf]
,55[coke][pepsi][^drpepper][somethingElse]
In the first, I'd like to capture 5
, 50
, and [foo,bar]
In the second, I'd like to capture 5
, [foo]
and [^apples]
and so on.
The pattern I came up with is:
/(\d+)?,?(\d+)?(\[\^?[^\]]+\])+/
but this only matches the numbers and the last term. If I remove the +
at the end, then it only matches the first term.