I have developed a UI that allows users to define a regex pattern and then a replacement string that is used by appendReplacement()
.
e.g.
- Pattern - 7(.*)
- Replace pattern - $1
So 71234 would yield 1234.
Is there anyway I can verify that the input pattern and replace pattern are compatible without the user having to enter a matching string? For example if the replace pattern was $1$2
then this would throw an exception at runtime. Can I check for this in advance?
EDIT:
Final solution was to do as aioobe suggested and get the group count by creating a dummy Matcher (not sure why this method isn't on Pattern TBH) and then manually parsing the replace string manually to find which $ group references are present. Bit fiddly but works.