Home > Java > javaTutorial > How Can I Use Java Regex to Match Patterns Not Preceded by Specific Characters?

How Can I Use Java Regex to Match Patterns Not Preceded by Specific Characters?

DDD
Release: 2024-12-04 09:59:12
Original
685 people have browsed it

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template