Regular expression Tutorial Mode modifier Use introduction _ Regular Expression

Source: Internet
Author: User
Tags modifier modifiers
Before we introduced the delimiters, atoms, and metacharacters in regular expressions, our basic syntax for regular expression tutorials left the pattern modifiers in regular expressions. This section will introduce the concept of the pattern modifier, the composition of the pattern modifier, as well as the demonstration of the pattern modifier of the example, after learning the content of this section, you can read the regular expression completely.

What is a pattern modifier?

1, the pattern modifier is a few letters, we can use one at a time in each regular expression, can also use more than one, each has a certain meaning.
2, the pattern modifier is used to tune the entire regular expression, or it can be said to extend the function of the regular expression.
Remember that formula for regular expressions? /Atom and metacharacters/pattern modifier ', where the forward slash is a boundary character.

composition of pattern modifiers
Pattern modifiers are letters, except that they have a special meaning in the application of pattern modifiers. Let me take a look at the pattern modifiers, see the following table:

Pattern modifier Description
I Indicates that matching in and pattern is case-insensitive
M Treats a pattern as multiple lines, using ^ and $ to indicate that any row can start or end with a regular expression
S If you do not use this pattern to correct the symbol, the "." In the meta character The default cannot represent a newline symbol, which treats a string as a single line
X Whitespace negligible in the representation pattern
E Regular expressions must be used when preg_replace a function that replaces strings (say this function)
A Starts with a pattern string, equivalent to a meta character ^
Z Ends with a pattern string, equivalent to a unary character $
U The characteristic of regular expressions: the comparison of "greed", using this pattern modifier can eliminate greedy mode

Since I means matching is case-insensitive, in the following example, we no longer demonstrate, let's take a look at examples of other pattern modifiers.

1, mode modifier m.

Copy Code code 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 succeeded <br>";
Print_r ($arr);
} else {
echo "<font color= ' red ' > Regular expression {$pattern} and string {$string} match failed </font>";
}
?>

The result of the match was successful. Note: When using the pattern modifier m, the matching string is considered to be multiple lines rather than the default one, so any row will match successfully if it starts with ABC. However, if you can match a line preceded by a space, you cannot match! Unless you modify the matching pattern of the regular expression.
2, mode modifier S.
Copy Code code 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 succeeded <br>";
Print_r ($arr);
} else {
echo "<font color= ' red ' > Regular expression {$pattern} and string {$string} match failed </font>";
}
?>

The matching demerit was also successful. If you remove the pattern modifier s from the example above, the match will fail. Because the pattern modifier s sees the matching string as a single line, so this time, the "." In the meta character. You can indicate a newline symbol.
3, pattern modifier x.
Copy Code code 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 succeeded <br>";
Print_r ($arr);
} else {
echo "<font color= ' red ' > Regular expression {$pattern} and string {$string} match failed </font>";
}
?>

The result of this match was a failure. Because we used the pattern modifier x to cancel the space in the pattern. Note: We cannot use the pattern modifier to cancel the whitespace represented by \s.
4, Mode modifier A.
Copy Code code as follows:

<?php
$pattern = '/ac/a ';
$string = ' ACAHGYGHVBM ';
if (Preg_match ($pattern, $string, $arr)) {
echo "Regular expression <b>{$pattern}</b> and string <b>{$string}</b> match succeeded <br>";
Print_r ($arr);
} else {
echo "<font color= ' red ' > Regular expression {$pattern} and string {$string} match failed </font>";
}
?>

A regular expression means a string that matches the beginning of AC, and results in success.
The pattern modifier Z represents a match at the end of the string, and a is the same as the use of a, and we no longer demonstrate.
5, pattern modifier U.
This pattern modifier is very important! In regular expressions, it is "greedy" in itself. So what is greedy mode? The greedy mode means that the regular expression will continue to try the following match after finding the first match, and match the largest range string if a match is found. But sometimes this is not the result we want, so we need to eliminate the greedy pattern.
Let's look at an example of a greedy pattern first:
Copy Code code 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 succeeded <br>";
Print_r ($arr);
} else {
echo "<font color= ' red ' > Regular expression {$pattern} and string {$string} match failed </font>";
}
?>

This instance was meant to match welcome, but the result matched the entire string of Welcome to Phpfuns (Note that our string ' Welcome to Phpfuns ', whose beginning and ending exactly formed the pattern match of the regular expression, so that the match was successful), This is the greedy pattern of regular expressions. Of course, this is not the result we want.

Cancel Greedy mode

We can use the pattern modifier u and metacharacters to cancel the greedy pattern of regular expressions in two ways.
pattern modifier u Cancel greedy mode
Copy Code code 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 succeeded <br>";
Print_r ($arr);
} else {
echo "<font color= ' red ' > Regular expression {$pattern} and string {$string} match failed </font>";
}
?>

The meta character? Cancel greedy mode
Copy Code code 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 succeeded <br>";
Print_r ($arr);
} else {
echo "<font color= ' red ' > Regular expression {$pattern} and string {$string} match failed </font>";
}
?>

Notice the position of the metacharacters, we must end the greedy pattern before "" to reach our goal, match welcome!
In this section we introduce the pattern modifiers in regular expressions, the greedy patterns of regular expressions, and the methods used to demonstrate pattern modifiers in regular expressions. So far, the basic regular expression of the grammatical composition we have also finished learning. In the next section, we'll show you some common regular expressions that you can use for review.

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.