Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the skills of regular expression matching that does not contain certain strings

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the regular expression matching does not contain some strings of the skills, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand.

Often we come across that we want to find text that does not contain a string. The easiest thing for programmers to think of is to use ^ (hede) to filter the "hede" string in regular expressions, but this is the wrong way to write it. We can write this: [^ hede], but such a regular expression has a completely different meaning. It means that the string cannot contain the three but characters' hash. So what kind of regular expression can filter out information that does not contain the full "hello" string?

In fact, it is not 100% correct to say that inverse matching is not supported in regular expressions. Like this problem, we can use negative lookups to simulate inverse matching to solve our problem:

^ (?! hede).) * $

The above expression can filter out information that does not contain the 'hede' string. As I said above, this is not the "good" use of regular expressions, but it can be used this way.

explain

A string is made up of n characters. Before and after each character, there is an empty character. In this way, a string of n characters has 1 empty string. Let's look at the string "ABhedeCD":

The positions of all e numbers are empty characters. Expression (?! hede). Will look forward to see if there is no "hede" string, if not (other characters), then. (period) will match these other characters. The "lookup" of this regular expression is also called "zero-width-assertions" (zero-width assertion) because it does not capture any characters, just judging.

In the above example, each null character checks to see if the string preceding it is not 'hede',. If not, this. (dot) is the match that captures this character. Expression (?! hede). Execute only once, so let's group the expression in parentheses and decorate it with * (asterisk)-- match 0 or more times:

((?! hede)).

You can understand that the regular expression ((?! hede).) * matches the result false of the string "ABhedeCD" because at E3, the (?! hede) match is not qualified, it is preceded by the "hede" string, that is, it contains the specified string.

In regular expressions,?! Whether to look forward or not, it helps us solve the problem of string "does not contain" matching.

Here are some additions:

Share the three methods of php to generate random numbers, generate non-repetitive random numbers between 1-10, and php generate examples of non-repetitive random numbers, refer to friends who need them.

See regex golf on hacker news, several interesting regular expression questions, some of which need to mismatch, such as matching a string that does not contain a word.

Before we get to the point, let's take a look at the syntax of regular expressions:

[abc] an or b or c. Any single character a? Zero or one a

[^ abc] any character that is not abc\ s space a * zero or more a

Any character\ S not a space a + one or more an of [a Muz] a z

[a-zA-Z] a murz or Amurz\ d any number a {n} happens to be n times a

At the beginning of ^ line\ D any non-numeric a {n,} appears at least n times a

At the end of a line\ w any alphanumeric or underscore a {ncentine m} appears nmerm times a

(.) Parentheses are used to group\ W any non-alphanumeric or underscore axioms? Zero or more a (non-greedy)

(a | b) An or b word boundary (a).\ 1 reference grouping

(? = a) is preceded by a (?! a) there is no a\ B non-word boundary.

There are (? = a) and (?! a) in the regular expression to indicate whether we need to match something.

So, you can use (?! a) when you need to mismatch something. For example, you can write this to match a string that does not contain hello.

^ (?!. * hello)

Here. * is used to indicate that there may be other characters before hello, so why add ^, because if it is not added, it may match to the position after h.

Now we can solve the problem of abba on regex golf.

The problem is to match words that do not contain the form of abba, such as abba,anallagmatic.

Regular expression code:

^ (?!. * (.) (.)\ 2\ 1)

Then using the mismatch, we can also solve the prime problem, which is equipped with prime x strings, so first look at the regularity.

^ (?! (xx+)\ 1cm $)

(xx+) is to match 2 or more x, (xx+)\ 1 + is to match the repeated occurrence of 2 or more strings, so (xx+)\ 1 + represents those non-prime strings, then the prime string is to remove these non-prime strings, that is, the above regular expression.

Thank you for reading this article carefully. I hope the article "what are the skills of regular expression matching that does not contain certain strings" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report