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

烂漫一生 提交于 2019-12-13 02:11:27

问题


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?


回答1:


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();



回答2:


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/


来源:https://stackoverflow.com/questions/1814036/is-there-a-way-to-use-a-list-of-string-parameters-with-a-regular-expression-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!