I am not sure if this is possible to do, but I need a way to replace a value of a numbered group specified in the my regex expression with a string declared dynamically at runtime, once a match has been made.
Given a simple case, something like…
(/)?([A-Za-z0-9])?(/)?$
I would want to be able to plugin a replacement for group 2.
I am currently using Java’s Matcher class.
Yes, that’s doable. Check out my answer to this question to see how. In fact, this question probably should be closed as a duplicate.
You’ll need to change the regex a little. I can’t tell exactly what you’re trying to do, so I can’t give any specifics, but at the very least you should move all those question marks inside the groups.
But it will still match an empty substring at the end of the target string, because everything is optional except the anchor,
$. Is that really what you meant to do?