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 is the basis of regular expressions in PHP?

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what is the basis of regular expressions of PHP". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

To be exact, a regular expression is a logical formula for manipulating a string, that is, using some specific characters defined at the beginning and a combination of these specific characters to form a "regular string". This "regular string" is actually used to filter strings. It can be understood that when we log in to users, we sometimes have to fill in specific data such as CAPTCHA or phone, and then we need to use regular expressions.

Although regular expressions look complex, they are not difficult, so let's take a look at them.

Delimiter of regular expression

The first thing we need to learn is the delimiter of the regular expression, as the name implies, the delimiter is the symbol that determines the boundary of the regular expression, and within the boundary is the regular expression. At the same time, the delimiter of the regular expression is specified:

The delimiter cannot be used with a-zA-Z0-9\ all else can be used. And must appear in pairs, where there is a beginning, there is an end.

Examples are as follows:

$regular expression $% regular expression% / regular expression /

What we need to pay attention to is that / is the escape character. When we need to match / in the rule, we can use\ to escape. If you find it troublesome, you can directly use other delimiters such as:

$/ $

Atoms of regular expressions

The atom of the regular expression is the smallest unit in the regular expression, that is, what we need to match. In our established integer expression, there must be at least one atom.

In fact, it can be understood that all visible and invisible characters are atoms, such as spaces, carriage returns, line feeds, 0-9, punctuation, A-Za-z, and Chinese.

Preg_match () function

Before we talk about atoms in detail, let's take a look at a function, that is, preg_match.

The preg_match () function in PHP searches and matches strings based on defined regular expressions.

The syntax format is as follows:

Preg_match (string $regular, string $string [, array & $result])

According to the $regular, that is, the regular expression we define, when matching the $string, the number of matches is returned if it exists, and then the matching result is put in the $result. If there is no match, then return 0.

Let's look at this through an example:

Output result:

As you can see from the above example, we defined the variable a, hoping to match a, which happens to exist in $b, and the output is successful through the if else statement.

Let's take another example:

Output result:

In the above example, you want to match to a string, but it does not exist in $b, so the match is not successful, and it is output as a success through the if else statement.

Once we know the basic usage of the preg_match () function, we can use it in combination with specially identified atoms.

Specially identified atom

\ dMutual-match a 0-9

\ DMusure-all characters except 0-9

\ w---a-zA-Z0-9 _

\ Wmurmuri-all characters except 0-9AmurZaMusz _

\ sMeltel-matches all white space characters\ n\ t\ r spaces

\ Smurmure-matches all non-white space characters

[]-specified range of atoms

Let me give you an example of their specific usage:

Output result:

In the above example, the specially identified atom\ d represents the number 0-9, so there is a 4 in the $b that needs to be matched, so the match is successful.

Output result:

In the above example, the specially identified atom\ w represents a-zA-Z0-9 _, and there is no corresponding element in the variable b, so the output is unmatched.

There is also a [^] character that does not match the specified range.

Examples are as follows:

Output result:

Characters other than 0-9AmurZamurz _ were matched by [^] characters, but not matched.

To sum up:

\ W Murray-[a-zA-Z0-9]

\ Wmurmuri-[^ a-zA-Z0-9 _]

\ dMutual-[0-9]

\ DMurray-[^ 0-9]

\ smurmuri-[\ t\ n\ f\ r]

\ Smurmuri-[^\ t\ n\ f\ r]

Metacharacters of regular expressions

In the above example, we can see that only one character can be matched by matching, but in our daily use, we usually match multiple characters, so we can't achieve our goal only through our atoms. We need to use metacharacters to help us modify the atom and achieve more functions.

*-represents matching the preceding atom, matching the preceding character 0 or any number of times.

+-matches one or more preceding characters

?-the preceding character is optional [optional] yes or no

.-more standard, points should be counted as atoms. Matches all characters except\ nor. Note: it has the lowest priority.

^-must start with a string after the circumflex character

$- must end with a character before $

\ bMurray-word boundary

\ Bmurmuri-non-border

{m}-Yes and only m times

{n ~ m}-can occur n to m times

{m,}-at least m times, the maximum number is unlimited

()-change the priority or treat a string as a whole, and you can also use it to extract the matched data.

Let's take a look at the use of these metacharacters through some examples:

Output result:

Through the addition of metacharacter +, multiple characters are matched. D in\ d + is a matching number, and + means to match the previous character at least once.

Pattern modifiers for regular expressions

Through the understanding of atoms and metacharacters, we have completed the introduction to regular expressions, but this still does not represent the true strength of regular expressions. What if we only want regular expressions to match part of them? Some special cases still need to be dealt with, so we need to use the pattern modifier of the regular expression.

Here are some common pattern modifiers:

The characters in I mode will match both uppercase and lowercase letters.

M string is treated as multiple lines

S treats a string as a single line and a newline character as an ordinary character.

X ignores the whitespace in the pattern.

A forces matching only from the beginning of the target string.

The dollar metacharacter in D mode matches only the end of the target string.

U matches the most recent string.

Its usage is as follows:

/ regular expression / pattern modifier

Let's take a look at its use with some examples:

Output result:

I can make the match match case at the same time, then change the matching $b to $c and try it. Let's take a look at the output:

Output result:

This is the end of the content of "what is the basis of regular expressions for PHP". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report