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

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:

(?!匹配模式)

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).*?$

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).*?$

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)(.*?)$

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).*$

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment