Home > Java > JavaBase > body text

Java uses regular expressions to match strings that do not contain a certain rule

王林
Release: 2019-11-19 16:16:56
Original
7904 people have browsed it

Java uses regular expressions to match strings that do not contain a certain rule

Test data:

Java uses regular expressions to match strings that do not contain a certain rule

For example, in the above simple log entries, we want to achieve two goals:

1. Filter out the data on No. 8;

2. Find the entries that do not contain the robots.txt string (as long as the URL contains robots.txt, all will be filtered out).

The look-ahead syntax is:

(?!匹配模式)
Copy after login

Let’s first achieve the first goal - matching entries that do not start with a specific string.

Here we want to exclude a continuous string, so the matching pattern is very simple, which is 2009-07-08. The implementation is as follows:

^(?!2009-07-08).*?$
Copy after login

Using Expresso, we can see that the results indeed filter out the data on No. 8.

Next, let’s achieve the second goal—exclude entries containing specific strings.

According to the way we wrote it above, I took a look at it:

^.*?(?!robots\.txt).*?$
Copy after login

The description of this regular rule in vernacular is: any character at the beginning, and then do not follow the continuous string of robots.txt. Then followed by any number of characters, the end of the string.

Run the test and found that:

Java uses regular expressions to match strings that do not contain a certain rule

did not achieve the effect we wanted. Why is this? Let’s add two capture groups to the above regular expression to debug it:

^(.*?)(?!robots\.txt)(.*?)$
Copy after login

Test results:

Java uses regular expressions to match strings that do not contain a certain rule

We see that, first The first group matched nothing, but the second group matched the entire string. Let’s go back and analyze the regular expression just now.

In fact, when the regular engine parses area A, it has already started to perform the look-ahead work of area B. At this time, it was found that the match was successful when area A was Null - .* originally allowed to match null characters, and the look-ahead condition was met. The string "2009" followed immediately after area A, not robots. Therefore, the entire matching process successfully matches all entries.

Java uses regular expressions to match strings that do not contain a certain rule

After analyzing the reason, we corrected the above regular expression and moved .*? into the look-ahead expression, as follows:

^(?!.*?robots).*$
Copy after login

Test result:

Java uses regular expressions to match strings that do not contain a certain rule

Recommended tutorial: Getting started with java development

The above is the detailed content of Java uses regular expressions to match strings that do not contain a certain rule. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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