使用正则表达式交换两个特定的单词
Swap two specific words using regex
我有这样一条短信:
boy girl loop for get out boy girl left right
我想用正则交换boy
和girl
。(注意:boy
/girl
是无序出现的。)所以我这样写:
String str = "boy girl loop for get out boy girl left right";
String regex = "(\bgirl\b)|(\bboy\b)";
System.out.println(str.replaceAll(regex, ""));
但是它不起作用。你能告诉我为什么并给出正确的解决方案吗?
您可以试试下面的代码。我只是在两者之间使用“临时”正则表达式来替换两个词。
String str = "boy girl loop for get out boy girl left right";
String regexGirl = "(girl)";
String regexBoy = "(boy)";
System.out.println(str.replaceAll(regexGirl, "temp").replaceAll(regexBoy, "girl").replaceAll("temp", "boy"));
您可以在替换中使用“回调”使用 Matcher#replaceAll
:
String str = "boy girl loop for get out boy girl left right";
Matcher m = Pattern.compile("\b(girl)\b|\b(boy)\b").matcher(str);
System.out.println( m.replaceAll(r -> r.group(2) != null ? "girl" : "boy") );
// => girl boy loop for get out girl boy left right
在这里,\b(girl)\b|\b(boy)\b
将整个单词 girl
匹配到第 1 组,boy
匹配到第 2 组。
r -> r.group(2) != null ? "girl" : "boy"
替换检查第 2 组是否匹配,如果不匹配,则替换为 girl
,否则为 boy
。
还有一种“用字典替换”的方法:
String[] find = {"girl", "boy"};
String[] replace = {"boy", "girl"};
Map<String, String> dictionary = new HashMap<String, String>();
for (int i = 0; i < find.length; i++) {
dictionary.put(find[i], replace[i]);
}
String str = "boy girl loop for get out boy girl left right";
Matcher m = Pattern.compile("\b(?:" + String.join("|", find) + ")\b").matcher(str);
System.out.println( m.replaceAll(r -> dictionary.get(r.group())) );
// => girl boy loop for get out girl boy left right
参见 this Java demo。
我有这样一条短信:
boy girl loop for get out boy girl left right
我想用正则交换boy
和girl
。(注意:boy
/girl
是无序出现的。)所以我这样写:
String str = "boy girl loop for get out boy girl left right";
String regex = "(\bgirl\b)|(\bboy\b)";
System.out.println(str.replaceAll(regex, ""));
但是它不起作用。你能告诉我为什么并给出正确的解决方案吗?
您可以试试下面的代码。我只是在两者之间使用“临时”正则表达式来替换两个词。
String str = "boy girl loop for get out boy girl left right";
String regexGirl = "(girl)";
String regexBoy = "(boy)";
System.out.println(str.replaceAll(regexGirl, "temp").replaceAll(regexBoy, "girl").replaceAll("temp", "boy"));
您可以在替换中使用“回调”使用 Matcher#replaceAll
:
String str = "boy girl loop for get out boy girl left right";
Matcher m = Pattern.compile("\b(girl)\b|\b(boy)\b").matcher(str);
System.out.println( m.replaceAll(r -> r.group(2) != null ? "girl" : "boy") );
// => girl boy loop for get out girl boy left right
在这里,\b(girl)\b|\b(boy)\b
将整个单词 girl
匹配到第 1 组,boy
匹配到第 2 组。
r -> r.group(2) != null ? "girl" : "boy"
替换检查第 2 组是否匹配,如果不匹配,则替换为 girl
,否则为 boy
。
还有一种“用字典替换”的方法:
String[] find = {"girl", "boy"};
String[] replace = {"boy", "girl"};
Map<String, String> dictionary = new HashMap<String, String>();
for (int i = 0; i < find.length; i++) {
dictionary.put(find[i], replace[i]);
}
String str = "boy girl loop for get out boy girl left right";
Matcher m = Pattern.compile("\b(?:" + String.join("|", find) + ")\b").matcher(str);
System.out.println( m.replaceAll(r -> dictionary.get(r.group())) );
// => girl boy loop for get out girl boy left right
参见 this Java demo。