我正在编写一个正则表达式以匹配"日本"的每一次出现,并将其替换为"日本"..为什么以下不起作用?并且"日本"可以在句子中和句子中的任何地方多次出现.我想替换所有出现的事件
public static void testRegex()
{
String input = "The nonprofit civic organization shall comply with all other requirements of section 561.422, japan laws, in obtaining the temporary permits authorized by this act.";
String regex = "japan";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
System.out.println(input.matches(regex));
System.out.println(input.replaceAll(regex, "Japan"));
}
Run Code Online (Sandbox Code Playgroud)
你不需要这里的正则表达式,也不需要Pattern和Matcher类.简单使用String.replace()将工作正常:
input = input.replace("japan", "Japan");
Run Code Online (Sandbox Code Playgroud)