The replacement of a simple preg_match on the Internet is unknown.

Source: Internet
Author: User
A simple preg_match replacement on the Internet $ string & nbsp ;=& nbsp; "Is & nbsp; is & nbsp; the & nbsp; cost & nbsp; of & nbsp; of & nbsp; gasoline & nbsp; going & nbsp; up "; & nbsp; $ the replacement of a simple preg_match on pa is unknown.

$string = "Is is the cost of of gasoline going up up"; 
$pattern = "/\b([a-z]+) \\1\b/i"; 
if(preg_match($pattern, $string,$arr)){
print_r($arr);
echo preg_replace($pattern, '$1', $string);
}

Output

Array
(
    [0] => Is is
    [1] => Is
)
Is the cost of gasoline going up


In my understanding, preg_match ($ pattern, $ repalce, $ subject) uses pattern to match each group from subject, and then uses the display method specified by replace to re-output
$ Pattern = "/\ B ([a-z] +) \ 1 \ B/I" only matches Is and is
How can I output the entire sentence "Is the cost of gasoline going up" again?
When can I use $1 and \ 1 to obtain the sub-pattern matching content? what are the requirements in double quotation marks and single quotation marks? Php? Regular expression sharing:
------ Solution --------------------
Preg_match match once,

While
Mixed preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1 [, int & $ count])
When the third parameter is not specified, the default value is-1 (unlimited ).

Difference between $1 and \ 1:
\ 1 can be used in both the mode and replacement string
$1 can only be used to replace strings
------ Solution --------------------
Preg_match only matches the first match. to use preg_match_all for all matches
Replace: replace all matching items (the matching part is the same as preg_match_all)
$ And \ are just habits. Generally, the expressions in the same regular expression are close to perl.
Pay attention to the original \ for a single double quotation mark. for other non-ambiguity, press the original
------ Solution --------------------
$ Pattern ="
------ Solution --------------------
<[^>] +> (.*) ] +>
------ Solution --------------------
";
$ Pattern = "/<[^>] +> (. *) <\/[^>] +> /";
Is the same
If the delimiter is/, escape the/in the rule string.
Fortunately, php provides custom delimiters to make the rule string look comfortable.
If JavaScript is used, it would be less fortunate.

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.