【发布时间】:2023-03-18 06:57:01
【问题描述】:
我正在尝试处理文本并替换所有以“www”开头的出现。用某个词(在这种情况下是“香蕉”),但我想排除所有在“www”之前有“http://”的情况。
当我使用积极的前瞻时,它会起作用(只有 http://www 的情况会改变),但是当我使用消极的前瞻时 - 两个词都会改变。
你能帮我解决这个问题吗?
String baseString = "replace this: www.q.com and not this:http://www.q.com";
String positiveLookahead = baseString.replaceAll("(?:http://)www([^ \n\t]*)", "Banana");
String negativeLookahead = baseString.replaceAll("(?!http://)www([^ \n\t]*)", "Banana");
//positiveLookahead works (changes only the scond phrase), but positiveLookahead does not (changes both)
【问题讨论】:
-
你不应该在后面看,而不是在前面吗?
-
@khelwood 你是对的,请参阅 Wiktor 的回复。谢谢!
标签: java regex string string-matching