Suppose that we have a regex with a named capture group like this:
^(?<my_group>00[A-Za-z]{1}|0[A-Za-z]{2}|[A-Za-z]{3})$
In this case, "my_group" will contain either 00x or 0xx or xxx.
Now I want to store only "x-es" in it, so "my_group" should contain x/xx/xxx. But I cannot figure out how to achieve this behavior.
The only thing that kinda worked out was creating a separate "inner" named groups:
^(?:00(?<my_group1>[A-Za-z]{1})|0(?<my_group2>[A-Za-z]{2})|(?<my_group3>[A-Za-z]{3}))$
But in this case, I'm forced to use different group names because it's forbidden to use the same name in majority of the programming languages (but it could be ok since I use |
between them, and at most one of these parts can be matched).
How can it be solved? Note that I cannot throw "zeros" away since this regex is used for string matching before splitting to the groups (so only 00x/0xx/xxx can be passed initially).
You can use
text.matches("(?=.{3}$)0{0,2}(?<mygroup>[A-Za-z]{1,3})")
See the regex demo.
Details:
^
- start of string (implicit in String#matches
)(?=.{3}$)
- a positive lookahead that requires the string to contain only 3 chars0{0,2}
- zero to two 0
chars(?<mygroup>[A-Za-z]{1,3})
- Group "mygroup": one to three letters$
- end of string (implicit in String#matches
).