Regex for matching something if it is not preceded by something else

JavaRegex

Java Problem Overview


With regex in Java, I want to write a regex that will match if and only if the pattern is not preceded by certain characters. For example:

String s = "foobar barbar beachbar crowbar bar ";

I want to match if bar is not preceded by foo. So output would be:

barbar
beachbar
crowbar
bar

Java Solutions


Solution 1 - Java

You want to use negative lookbehind like this:

\w*(?<!foo)bar

Where (?<!x) means "only if it doesn't have "x" before this point".

See Regular Expressions - Lookaround for more information.

Edit: added the \w* to capture the characters before (e.g. "beach").

Solution 2 - Java

Another option is to first match optional word characters followed by bar, and when that has matched check what is directly to the left is not foobar.

The lookbehind assertion will run after matching bar first.

\w*bar(?<!foobar)
  • \w* Match 0+ word characters
  • bar Match literally
  • (?<!foobar) Negative lookbehind, assert from the current position foobar is not directly to the left.

Regex demo

Solution 3 - Java

In some cases, it could be easier to optionally include the preceding part, then skip those matches in a second step. For instance, to find numbers that don't start with a "+":

if (preg_match_all('/(\+?[0-9][0-9\s\-].*[0-9])/s',$text,$matches)) {
	foreach($matches[1] as $match) {
		if(substr($match,0,1) == '+'){
			continue;
		}
		// continue processing
	}
}

The negative look behind did not work since it would still match 2+ digits, but it would not include the first digit in the match. For instance +1234 would be returned as 234.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionb3bopView Question on Stackoverflow
Solution 1 - JavaAdam RoferView Answer on Stackoverflow
Solution 2 - JavaThe fourth birdView Answer on Stackoverflow
Solution 3 - JavaFrank ForteView Answer on Stackoverflow