Group indexes annotated
(.(\d)(-)(\d*).*)
1 2 3 4
Not sure what you expected here?
To match 19
and 24
into two separate groups, use something like this:
(\d+)-(\d+)
To also match the court name into a group, a non-greedy star works.
(.*?) (\d+)-(\d+)
I bet your initial regex was more like this:
(.*(\d*)(-)(\d*).*)
This will behave closer to what you describe, because
.
also matches digits, i.e. .*
will go up to the -
, and
*
is not required to match anything at all, i.e. \d*
it will happily match zero digits, causing group 2 to be empty.
Lessons:
- Don't use
*
when you expect at least something to be there. Prefer +
in this case.
- Be wary of the greedy star, especially when used with the unspecific
.
it can match things you did not intend.
- You don't have to put parenthesis around everything in regular expressions. Only add groups around things you want to extract (i.e. "capture"), or around things you want to match/fail/repeat as one (i.e. "make atomic").