Practical Regular Expression learning notes

Source: Internet
Author: User

// Match the text, which is useful occasionally, but be careful when the character contains \ E
$ Str = '[a-z]';
$ Str = preg_replace ('/\ G [a-z] \ E/', '', $ str );
Echo $ str; // print empty, all replaced, probably preg_quote Function

// Name the matching result so that the value can be obtained in the matching result.
$ Str = 'abc123abc ';
Preg_match ('/(? P <num> \ d +)/', $ str, $ arr );
Echo $ arr ['num']; // equivalent to echo $ arr [1]

// It is only used for grouping parentheses. The matching content is not captured by variables. Sometimes it is necessary to improve the execution efficiency.
$ Str = 'abc123abc ';
Preg_match ('/abc (? : \ D +)/', $ str, $ arr );
Echo $ arr [1]; // no $ arr [1] except $ arr [0]. \ 1 is not assigned.

// A good helper for inserting data. Search for the position of the forward and backward anchor, and add a comma for each 3 digits.
$ Str = 'fdfad123456789fdfd ';
$ Str = preg_replace ('/(? <= \ D )(? = (\ D {3}) + (?! \ D)/', $ str );
Echo $ str; // print fdfad123, 456,789 fdfd

// Match with the minimum number of results
$ Str = 123456;
Preg_match ('/\ d +/', $ str, $ arr );
Echo $ arr [0]; // everyone knows it's 123456.
Preg_match ('/\ d +? /', $ Str, $ arr );
Echo $ arr [0]; // This time is 1

// A useful one. You can determine whether the previous match exists. For example, in the following example, you can ignore the single quotation marks or double quotation marks on the right side of the equal sign.
$ Str = <HTML
<Font size = 12> </font>
<Font size = '13'> </font>
<Font size = "14"> </font>
<Font size = "15> </font>
HTML;
Preg_match_all ('/<font \ s + size = ([\' "]?) (\ D +) \ 1 [^>] *>/', $ str, $ arr );
Print_r ($ arr );
/*
Array
(
[0] => 12
[1] => 13
[2] => 14
)
*/

// Some pattern modifiers can also be placed in expressions
// Match the color values in the XHTML-compliant style. The uppercase STYLE is ignored, but the color in the style is case-insensitive.
$ Str = '<B style = "COLOR: red"> </B> <B STYLE = "color: blue"> </B> <B style = "color: green "> </B> ';
Preg_match_all ('/style = ([\' "]?) (? I) color :( \ w +) \ 1 (? -I)/', $ str, $ arr );
Print_r ($ arr [2])

// You can also put the Matching content in it, separated by:, so you don't need to end the write (? -I)
// Preg_match_all ('/style = ([\' "]?) (? I: color :( \ w +) \ 1/', $ str, $ arr );
// Let's look at another example.
$ Str = '<B> Style </B> ';
Preg_match ('/<B> (? I: style) <\/B>/', $ str, $ arr );
Print_r ($ arr); // can match
$ Str = '<B> Style </B> ';
Preg_match ('/<B> (? I: style) <\/B>/', $ str, $ arr );
Print_r ($ arr); // nothing matches

// Word search. Unfortunately, it can only be used in English
$ Str = 'I \'m a teacher ';
Preg_match_all ('/\ B [a-z] + \ B/I', $ str, $ arr );
Print_r ($ arr)

// U modifier, matching by unicode
$ Str = 'you ';
$ Str = preg_replace ('/[you]/', 'you', $ str );
Echo $ str; // split, Print four times you

// Check the effect after the u modifier is added below. The modifier must be UTF-8 encoded; otherwise, an error is returned.
// All my texts are gb2312, so convert them to UTF-8
$ Str = iconv ('gb2312', 'utf-8', 'you ');
$ Regex = iconv ('gb2312', 'utf-8', '/[you]/U ');
$ Str = preg_replace ($ regex, 'you', $ str );
Echo $ str; // print twice you

// The x-mode modifier. You can ignore the blank space and add comments.
$ Str = 'test test ';
Preg_match ('/test # match only test/X', $ str, $ arr );
Print_r ($ arr );

// Exclude the surround view (? <!...) (?!...) Ignore priority *? +? ?? Combination
$ Str = 'test <B> test1 <B> test2 </B> ';
Preg_match ('/<B> (? :.(? <! <B>) * <\/B>/I ', $ str, $ arr );
// Or preg_match ('/<B> (? :(?! <B>).) * <\/B>/I ', $ str, $ arr );
Print_r ($ arr)

// At that time, this write cannot cope with $ str = 'test <B> test1 <B> test2 </B> test3 </B> ';
// Rewrite the regular expression to preg_match_all ('/<B> (? :(?! <\/? B>).) * <\/B>/I ', $ str, $ arr );
// Complete the simplest UBB replacement based on the above steps
$ Str = 'test [B] test1 [B] test2 [/B] test3 [/B] test ';
$ Str = preg_replace ('/\ [B \] (? :(?! \[\/? B \]).) *) \ [\/B \]/I ',' <B> \ 1 </B> ', $ str );
$ Str = preg_replace ('/\ [B \] (? :(?! \[\/? B \]).) *) \ [\/B \]/I ',' <B> \ 1 </B> ', $ str );
Print_r ($ str)

// If no matching result is returned after confirmation, you can use the curing group to discard the backup status to improve efficiency.
$ Str = 'subobject ';
Preg_match ('/(\ w +):/', $ str, $ arr );

// Use the following method instead
// The Match ends when the first matching rule matches t at the end of the text. Enable the second matching rule: the result is not found. Therefore, the result is retrieved, but \ w does not include :, so you can give up directly,
Preg_match ('/(?> \ W +):/', $ str, $ arr );

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.