In Java RegEx I have the following:
(1abc\\d{2})|(2abc\\d{3})|(3abc\\d{4})
I would like to extract the ‘abc\d’ out of the RegEx and replace the RegEx with something like:
(1|2|3)abc\\d({2]|{3}|{4})
The problem is that 1 belongs to {2} and 2 belongs to {3} and 3 belongs to {4}. So a good match is, 1abc12, but a bad match is 1abc123.
I have recently learned RegEx and I feel like i’m missing some knowledge about RegEx to make this possible. Is it even possible?
What you describe is not possible with regular expressions. In general, a later part of the expression cannot depend on the matching result of an earlier part of the expression. For example, you can’t write a regex that matches balanced parenthesis or matching HTML tags.
Some implementations provide extensions which give exceptions to this (irregular expressions), but I don’t think they apply here.