Regular Expression tutorial-Introduction to pattern Modifiers

Source: Internet
Author: User

Previously, we introduced the delimiters, atoms, and metacharacters in regular expressions, so the basic syntax of the regular expression tutorial leaves the pattern modifier in the regular expression. This section describes the concept of a pattern modifier, the composition of a pattern modifier, And the demo of a pattern modifier based on an instance. After learning this section, you can fully understand the regular expression.

What is a pattern modifier?

1. The pattern modifier is a few letters. We can use one or more regular expressions at a time. Each pattern modifier has a certain meaning.
2. The pattern modifier is used to optimize the entire regular expression. It can also be said to be an extension of the regular expression function.
Do you still remember the regular expression formula? '/Atomic and metacharacter/pattern modifier', where the forward slash is the boundary character.

Pattern modifier Composition
The pattern modifier is a letter, but it has a special meaning in the application of the pattern modifier. Next let's take a look at all the pattern modifiers. See the following table:

Pattern Modifier Description
I It indicates that matching with the mode is case-insensitive.
M Treat the mode as multiple rows. Use ^ and $ to indicate that any row can start or end with a regular expression.
S If this mode is not used, the "." In the metacharacters cannot represent line breaks by default, and the strings are treated as single lines.
X Indicates that the white space in the mode is ignored.
E Regular Expressions can be used only when preg_replace is used to replace a string)
A It starts with a pattern string and is equivalent to a metacharacter ^
Z End with a pattern string, equivalent to a metacharacter $
U A regular expression is more greedy. This pattern modifier can be used to cancel the greedy pattern.

Because I indicates that the matching is case insensitive, we will not demonstrate it in the following example. Let's take a look at the examples of other pattern modifiers.

1. The pattern modifier m.

Copy codeThe Code is as follows: <? Php
$ Pattern = '/^ abc/m ';
$ String = 'bcd
Abc
CBA ';
If (preg_match ($ pattern, $ string, $ arr )){
Echo "Regular Expression <B >{$ pattern} </B> and string <B >{$ string} </B> match successfully <br> ";
Print_r ($ arr );
} Else {
Echo "<font color = 'red'> Regular Expression {$ pattern} and string {$ string} failed to match </font> ";
}
?>

The matching result is successful. Note: When the pattern modifier m is used, the matching string is regarded as multiple rows rather than the default single row. Therefore, if any row starts with abc, the matching is successful. However, if there is a space in front of the line that can be matched, it cannot be matched! Unless the matching mode of the regular expression is modified.
2. Pattern modifier s. Copy codeThe Code is as follows: <? Php
$ Pattern = '/a. * c/s ';
$ String = 'adsadsa
C ';
If (preg_match ($ pattern, $ string, $ arr )){
Echo "Regular Expression <B >{$ pattern} </B> and string <B >{$ string} </B> match successfully <br> ";
Print_r ($ arr );
} Else {
Echo "<font color = 'red'> Regular Expression {$ pattern} and string {$ string} failed to match </font> ";
}
?>

This match is successful. If you remove the pattern modifier s from the previous example, the matching will fail. Because the pattern modifier s regards the matching string as a single line, "." In the metacharacters can represent the line break symbol.
3. The pattern modifier x. Copy codeThe Code is as follows: <? Php
$ Pattern = '/a c/X ';
$ String = 'a C ';
If (preg_match ($ pattern, $ string, $ arr )){
Echo "Regular Expression <B >{$ pattern} </B> and string <B >{$ string} </B> match successfully <br> ";
Print_r ($ arr );
} Else {
Echo "<font color = 'red'> Regular Expression {$ pattern} and string {$ string} failed to match </font> ";
}
?>

This matching result fails. Because we use the pattern modifier x to cancel spaces in the pattern. Note: we cannot use the pattern modifier to cancel the white space represented by \ s.
4. Pattern modifier. Copy codeThe Code is as follows: <? Php
$ Pattern = '/ac/';
$ String = 'acahgyghvbm ';
If (preg_match ($ pattern, $ string, $ arr )){
Echo "Regular Expression <B >{$ pattern} </B> and string <B >{$ string} </B> match successfully <br> ";
Print_r ($ arr );
} Else {
Echo "<font color = 'red'> Regular Expression {$ pattern} and string {$ string} failed to match </font> ";
}
?>

The regular expression indicates that the string starting with ac is matched and the result is successful.
The pattern modifier Z indicates the matching at the end of the string, which is the same as the usage of A. We will not demonstrate it any more.
5. The pattern modifier U.
This pattern modifier is very important! In a regular expression, it is greedy. So what is the greedy model? Greedy Mode means that the regular expression will continue to try the matching after finding the first match by default. If matching can be found, it will match the maximum range string. But sometimes this is not what we want, so we need to cancel the greedy mode.
Let's take a look at an example of greedy mode:Copy codeThe Code is as follows: <? Php
$ Pattern = '/<B>. * <\/B> /';
$ String = '<B> welcome </B> <B> to </B> <B> phpfuns </B> ';
If (preg_match ($ pattern, $ string, $ arr )){
Echo "Regular Expression <B >{$ pattern} </B> and string <B >{$ string} </B> match successfully <br> ";
Print_r ($ arr );
} Else {
Echo "<font color = 'red'> Regular Expression {$ pattern} and string {$ string} failed to match </font> ";
}
?>

This instance is intended to match welcome, but the result matches the entire string of welcome to phpfuns (note that our string 'Welcome to phpfuns ', the regular expression matches the regular expression pattern, so the match is successful. This is the greedy pattern of the regular expression. Of course, this is not our result.

Cancel greedy Mode

Can we use the pattern modifier U and metacharacters? Two ways to cancel the greedy pattern of a regular expression.
Mode modifier U cancels greedy Mode Copy codeThe Code is as follows: <? Php
$ Pattern = '/<B>. * <\/B>/U ';
$ String = '<B> welcome </B> <B> to </B> <B> phpfuns </B> ';
If (preg_match ($ pattern, $ string, $ arr )){
Echo "Regular Expression <B >{$ pattern} </B> and string <B >{$ string} </B> match successfully <br> ";
Print_r ($ arr );
} Else {
Echo "<font color = 'red'> Regular Expression {$ pattern} and string {$ string} failed to match </font> ";
}
?>

Metacharacters? Cancel greedy Mode Copy codeThe Code is as follows: <? Php
$ Pattern = '/<B> .*? <\/B> /';
$ String = '<B> welcome </B> <B> to </B> <B> phpfuns </B> dsadsadas ';
If (preg_match ($ pattern, $ string, $ arr )){
Echo "Regular Expression <B >{$ pattern} </B> and string <B >{$ string} </B> match successfully <br> ";
Print_r ($ arr );
} Else {
Echo "<font color = 'red'> Regular Expression {$ pattern} and string {$ string} failed to match </font> ";
}
?>

Pay attention to the metacharacters. We must end the greedy mode before "" to achieve our goal, matching welcome!
This section describes the pattern modifiers in regular expressions, the greedy pattern of regular expressions, and the usage of pattern modifiers in regular expressions. So far, we have learned the basic regular expression syntax structure. In the next section, we will introduce some common Regular Expressions for your reference.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.