Home >Java >javaTutorial >How Can I Use Java Regex to Match Patterns Not Preceded by Specific Characters?
Matching Patterns Not Preceded by Specified Characters with Regex
In Java, using regular expressions, you can match patterns only if they are not preceded by specific characters. Consider the following example:
String s = "foobar barbar beachbar crowbar bar ";
You want to match every occurrence of "bar" that is not preceded by "foo". The matching results should be:
barbar
beachbar
crowbar
bar
To achieve this, you can employ negative lookbehinds, which are denoted by the pattern (?
In this case, the regular expression that accomplishes your goal is:
w*(?
The (?
The above is the detailed content of How Can I Use Java Regex to Match Patterns Not Preceded by Specific Characters?. For more information, please follow other related articles on the PHP Chinese website!