2

正如标题所示,我只想替换以#开头的内容并跳过以!开头的内容 这是代码片段。它不会跳过以!开头的单词

String test = "Hello #Admin  Welcome this is Your welcome page !#Admin  This is #Admin"

注意:- 替换时必须跳过!#Admin。

String out = test.replaceAll("#Admin", "MyAdministrator");
log.debug("OutPut: "+out);

OutPut: Hello MyAdministrator  Welcome this is Your welcome page !MyAdministrator  This is MyAdministrator

我怎么能忽略以感叹号开头的单词。

谢谢。

4

2 回答 2

3

使用负面的回顾?

String test = "Hello #Admin  Welcome this is Your welcome page !#Admin  This is #Admin";
String out = test.replaceAll("(?<!!)#Admin", "MyAdministrator");
System.out.println("OutPut: "+out);

往后看是(?<!!)

于 2013-06-27T08:52:00.840 回答
2

试试这个

    String out = test.replaceAll("(?<!!)#Admin", "MyAdministrator");

这被称为负向后看,请参阅 Pattern API

于 2013-06-27T08:52:21.263 回答