tags:

views:

72

answers:

2

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? Thanks in advance.

+1  A: 

Most regex implementations allow you to do something like this in a search and replace:

s/The quick (red|brown|blue|yellow) fox (jumps|leaps) over the lazy (dog|cat)/The quick $1 fox $2 over the lazy $3/
ennuikiller
+1  A: 

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(strings[i]);
    sb.append(literals[i + 1]);
}

// => sb.toString();
Josef
for longer strings you might want to use a compiled `Pattern` with a `Matcher` instead of `split`
Josef