正如标题所示,我只想替换以#开头的内容,并跳过以开头的内容!以下是代码段。它不会跳过以开头的单词!#
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
如何忽略以感叹号开头的单词。
感谢。
答案 0 :(得分: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);
后视是(?<!!)
。
答案 1 :(得分:1)
试试这个
String out = test.replaceAll("(?<!!)#Admin", "MyAdministrator");
这称为负面反馈,参见Pattern API