The Comparison Between POSIX and Perl-Compatible Regular Expressions (preg_match, preg_replace,

Source: Internet
Author: User
Tags ereg
The Comparison Between POSIX and Perl-Compatible Regular Expressions (preg_match, preg_replace, ereg, and ereg_replace). For more information, see.

The Comparison Between POSIX and Perl-Compatible Regular Expressions (preg_match, preg_replace, ereg, and ereg_replace). For more information, see.

First, let's take a look at the two main functions of POSIX-style Regular Expressions:

Ereg function: (Regular Expression matching)

Format: int ereg (string pattern, string [, array & regs])
Note: The preg_match () function using Perl Compatible with regular expression syntax is usually a faster alternative than ereg. (Generally, preg_match () is used, which is better ~~)

Search for substrings matching the given regular expression pattern in string in case-sensitive mode. If you find a substring that matches the child pattern in the parentheses in pattern and the function call provides the third parameter regs, the matched substring is saved to the regs array. $ Regs [1] contains the substring starting with the first left parentheses, $ regs [2] contains the second substring, and so on. $ Regs [0] contains the entire matched string.

Return Value: if the pattern matching is found in the string, the length of the matched string is returned. If no matching is found or an error occurs, FALSE is returned. If the optional parameter regs is not passed or the matched string length is 0, this function returns 1.

Let's take a look at the example of the ereg () function:

The following code snippet accepts date (YYYY-MM-DD) in ISO format and then displays in DD. MM. YYYY format:

The Code is as follows:


If (ereg ("([0-9] {4})-([0-9] {1, 2})-([0-9] {1, 2 })", $ date, $ regs )){
Echo "$ regs [3]. $ regs [2]. $ regs [1]";
} Else {
Echo "Invalid date format: $ date ";
}
?>


Bytes -----------------------------------------------------------------------------------
Ereg_replace function: (Regular Expression replacement)

Format: string ereg_replace (string pattern, string replacement, string)
Function Description:
This function scans the part matching pattern in string and replaces it with replacement.
Returns the replaced string. (If there is no matching item to be replaced, the original string is returned .)
If pattern contains substrings in parentheses, replacement can contain substrings, for example, \ digit, these substrings are replaced with the substrings in parentheses represented by numbers; \ 0 contains the entire content of the string. A maximum of nine substrings can be used. Parentheses can be nested. In this case, the left parentheses are used to calculate the order.
If no match is found in the string, the string is returned as is.
Let's take a look at this function example:
1. the following code snippet outputs "This was a test" three times:

The Code is as follows:


$ String = "This is a test ";
Echo str_replace ("is", "was", $ string );
Echo ereg_replace ("() is", "\ 1was", $ string );
Echo ereg_replace ("() is)", "\ 2was", $ string );
?>


Note that if an integer or website space is used in the replacement parameter, the expected results may not be obtained. This is because ereg_replace () interprets and applies numbers as the sequence values of characters. For example:
2. replacement:

The Code is as follows:


/* Expected results cannot be generated */
$ Num = 4;
$ String = "This string has four words .";
$ String = ereg_replace ('four ', $ num, $ string );
Echo $ string;/* Output: 'This string has words .'*/
/* This example works normally */
$ Num = '4 ';
$ String = "This string has four words .";
$ String = ereg_replace ('four ', $ num, $ string );
Echo $ string;/* Output: 'This string has 4 words .'*/
?>


3. Replace the URL with a hyperlink:

The Code is as follows:

$ Text = ereg_replace ("[[: alpha:] +: // [^ <> [: space:] + [[: alnum:]/]",
"\ 0", $ text );


Tip: The preg_replace () function uses Perl-Compatible Regular Expression syntax, which is usually a faster alternative than ereg_replace.
Let's take a look at the two main functions compatible with regular expressions in Perl:
Preg_match function: (Regular Expression matching)
Format: int preg_match (string pattern, string subject [, array matches [, int flags])
Function Description:
Search for the content that matches the regular expression given by pattern in the subject string.
If matches is provided, it is filled with the search results. $ Matches [0] will contain the text that matches the entire pattern, $ matches [1] will contain the text that matches the child pattern in the first captured bracket, and so on.
Flags can be the following mark:
PREG_OFFSET_CAPTURE
If this flag is set, the offset of the affiliated string is also returned for each matching result. Note that this changes the value of the returned array, so that each unit is also an array. The first item is the matching string, and the second item is its offset. This tag is available from PHP 4.3.0.
The flags parameter is available since PHP 4.3.0.
Preg_match () returns the number of times pattern matches. Either 0 (no match) or 1 time, because preg_match () will stop searching after the first match. Preg_match_all () indicates that, on the contrary, the end of the subject is always searched. If an error occurs in preg_match (), FALSE is returned.
Tips: If you only want to check whether a string is contained in another string, do not use preg_match (). It can be replaced by strpos () or strstr (), which is much faster.
Let's take a look at its example:
Example 1. Search for "php" in the text ":

The Code is as follows:


// The "I" after the pattern delimiter indicates a search that is case-insensitive.
If (preg_match ("/php/I", "PHP is the web scripting language of choice .")){
Print "A match was found .";
} Else {
Print "A match was not found .";
}
?>


Example 2. Search for the word "web ":

The Code is as follows:


In/* mode, \ B indicates the word boundary. Therefore, only independent "web" words are matched,
* Does not match a part of "webbing" or "cobweb */
If (preg_match ("/\ bweb \ B/I", "PHP is the web scripting language of choice .")){
Print "A match was found .";
} Else {
Print "A match was not found .";
}
If (preg_match ("/\ bweb \ B/I", "PHP is the website scripting language of choice .")){
Print "A match was found .";
} Else {
Print "A match was not found .";
}
?>


Example 3. Retrieve the domain name from the URL:

The Code is as follows:

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.