专家非常简单的正则表达式

时间:2016-04-08 14:17:09

标签: java regex

我想在文本中使用findreplace个占位符(使用正则表达式)。

String s = "This is a test {replace_me}. And this is another {replace_me_too}."

s.replaceFirst(regex, newString);

当我想要替换像"{*}"这样的字符串的第一个出现时,正则表达式会是什么样子。

我试过这个

url = url.replaceFirst(Pattern.quote("{") + "*" + Pattern.quote("}"), param);

1 个答案:

答案 0 :(得分:1)

如果需要,您可以使用replaceAll()功能替换所有出现的事件。

Java代码

String REGEX = "\\{[^}]*\\}";
String INPUT = "This is a test {replace_me}. And this is another {replace_me_too}.";
String REPLACE = "xxx";
Pattern p = Pattern.compile(REGEX);

Matcher m = p.matcher(INPUT); 
INPUT = m.replaceFirst(REPLACE);
System.out.println(INPUT);

<强> Ideone Demo

相关问题