我想匹配一个单词" some" in"我想要一些有些馅饼"并替换任何字符串匹配它与{..}的串联,所以它的输出应该是"我想要{somesomesome}馅饼"但我的是#34;我想要{有些}馅饼"。
这是我的正则表达式:
(一些)*一些
答案 0 :(得分:0)
以下一个样本:
String some = "some";
String input = "i want somesomesome pies, and some apples, and somesomesomesome pears";
// at least one 'some' word
System.out.println(input.replaceAll("((?:"+some+")+)", "{$1}"));
// at least two 'some' word
System.out.println(input.replaceAll("((?:"+some+"){2,})", "{$1}"));
输出结果为:
i want {somesomesome} pies, and {some} apples, and {somesomesomesome} pears
i want {somesomesome} pies, and some apples, and {somesomesomesome} pears