mpobrien mpobrien - 8 months ago 20
Java Question

Is there a way to use a list of string parameters with a regular expression (with groups) to construct a new string?

Let's say for example that I have a regular expression like this:

"The quick (red|brown|blue|yellow) fox (jumps|leaps) over the lazy (dog|cat)."

This regex has 3 grouped components - if it's matched against a given string, the regex api would allow you to easily extract the value inside each group.

Now let's say I have 3 strings:

["red", "leaps","cat"]

If we make the assumption that all the characters in the regex that are not inside groups are just literal text characters -
is there a way to then insert each of those 3 strings into the corresponding group in the original regex, resulting in an output string that combines the non-grouped portion of the regex? In this case, resulting in "The quick red fox leaps over the lazy cat."
Preferably, without needing to have a string that has already matched the regex.

I'm looking to do this in Java - I'm quite sure java.util.regex doesn't support this, but I thought maybe there would be a 3rd party lib out there that could allow this to be done. Can anyone give me some pointers?

Answer Source

As long as you can do without nested capturing groups you can simply use more regex to retrieve the literals:

String[] strings = new String[] { "red", "leaps", "dog" };
String[] literals = new String("The quick (red|brown|blue|yellow) fox " +
    "(jumps|leaps) over the lazy (dog|cat).").split("(?=[^\\\\])\\(.*?\\)");

StringBuilder sb = new StringBuilder(literals[0]);
for(int i = 0; i < strings.length; i++) {
    sb.append(literals[i + 1]);

// => sb.toString();