2

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)
Tal Sheffer
  • 155
  • 1
  • 10

1 Answers1

2

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).
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thanks, that's working :) So what does lookahead do? – Tal Sheffer Aug 23 '18 at 11:16
  • @TalSheffer A negative lookahead matches a location in the string that is not immediately *followed* with a pattern, so `(?!a)b` is always equal to `b` as `b` is not an `a`. – Wiktor Stribiżew Aug 23 '18 at 11:18
  • Got it. So what would be a legit use for a negative lookahead? – Tal Sheffer Aug 23 '18 at 11:34
  • @TalSheffer Plenty. For example, see [this today's answer of mine](https://stackoverflow.com/a/51982341/3832970). More on [negative lookahead here](https://stackoverflow.com/questions/31201690/find-word-not-followed-by/31201710#31201710), and [here](http://stackoverflow.com/questions/32886477/regexp-find-numbers-in-a-string-in-any-order/32886855#32886855). – Wiktor Stribiżew Aug 23 '18 at 11:37
  • Thank you very much! Much appriciated! – Tal Sheffer Aug 23 '18 at 14:17