I am trying to process text and replace all the occurence that start with "www." with a certain word (in this cases "Banana"), but I want to exclude all the cases in which there is "http://" before the "www".
When I use a positive lookahead it works (only the http://www case changes), but when I use a negative lookahead - both words change.
Can you help me with this one?
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)
Use a negative lookbehind , (?<!http://)
:
String negativeLookahead = baseString.replaceAll("(?<!http://)www[^ \n\t]*", "Banana");
The (?<!http://)www[^ \\n\\t]*
pattern matches:
(?<!http://)
- a location in the string that is not immediately preceded with http://
www
- a literal www
substring [^ \\n\\t]*
- any 0+ chars other than space, newline and carriage return (probably you want to try \\\\S*
instead).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.