PHP Regular Expression Learning notes _ regular expressions

Source: Internet
Author: User
Copy Code code as follows:

Match the text, this is occasionally easy to use, but be careful that the character contains \e
$str = ' [A-z] ';
$str = preg_replace ('/\g[a-z]\e/', ', ', $str);
Echo $str; Print empty, all replaced, related probably is preg_quote function
Copy content to Clipboard code:
Name the matching result so that it can be used to get the value in the matching result
$str = ' abc123abc ';
Preg_match ('/(? p<num>\d+)/', $STR, $arr);
echo $arr [' num ']; Equivalent to echo $arr [1]


Copy Code code as follows:

Parentheses for grouping only, matching content is not captured by variables, and sometimes needs to improve execution efficiency
$str = ' abc123abc ';
Preg_match ('/abc (?: \ d+)/', $STR, $arr);
echo $arr [1]; There is no $arr[1 except $arr[0], and will not give \1

Copy Code code as follows:

Insert a good helper, forward and backward anchor point search location, add a comma per 3 bits
$str = ' FDFAD123456789FDFD ';
$str = Preg_replace ('/<=\d) (? = (\d{3}) + (?! \d))/', ', ', $str);
Echo $str; Print FDFAD123,456,789FDFD

Match with the fewest results
$str = 123456;
Preg_match ('/\d+/', $str, $arr);
echo $arr [0]; Everybody knows it's 123456.
Preg_match ('/\d+?/', $str, $arr);
echo $arr [0]; This one's 1.
A more useful, you can determine whether the previous match, such as the example below, you can ignore the equal sign to the right there is a single quotation mark double quotes or nothing
$str = <<<font size=12></font>
<font size= ' ></font>
<font size= "></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
)
*/
Partial-mode modifiers, which can also be placed in an expression
This matches the color values in the style that conform to the XHTML specification, and the uppercase style is ignored, but the color inside can be 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 inside, with: separated, do not have to write the end (?-i)
Preg_match_all ('/style= ([\ ' "]?) (? I:color: (\w+)) \1/', $str, $arr);
Look at one more example
$str = ' <B>Style</B> ';
Preg_match ('/<b> i:style) <\/b>/', $str, $arr);
Print_r ($arr); Can match to
$str = ' <B>Style</b> ';
Preg_match ('/<b> i:style) <\/b>/', $str, $arr);
Print_r ($arr); There's no match.
Word retrieval, but only in English
$str = ' i\ ' m a teacher ';
Preg_match_all ('/\b[a-z]+\b/i ', $str, $arr);
Print_r ($arr)
Copy content to Clipboard code:
u modifier, matching by Unicode
$str = ' You You ';
$str = Preg_replace ('/[you]/', ' You ', $str);
Echo $str; was opened, printed 4 times for You
Look at the following plus the U modifier effect, the modifier needs to encode utf-8 or it will be an error
My text is gb2312, so I want to turn it into a utf-8.
$str = Iconv (' gb2312 ', ' utf-8 ', ' you ');
$regex = Iconv (' gb2312 ', ' utf-8 ', '/[you]/u ');
$str = Preg_replace ($regex, ' you ', $str);
Echo $str; Printed 2 times
X-mode modifier, you can ignore whitespace and add comments
$STR = ' test test ';
Preg_match ('/test #只匹配小写的test/x ', $str, $arr);
Print_r ($arr);
Copy content to Clipboard code:
Exclusion (?<!...) (?! ...), ignore priority *? +? ?? The composite use of
$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 the time, it couldn't handle $str = ' Test <B>test1<B> test2</b> test3</b> ';
Rewrite the regular can be Preg_match_all ('/<b>,:(?! <\/? b>).) *<\/b>/i ', $str, $arr);
According to the above to complete a simplest UBB replacement
$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 you have confirmed that the back does not have a match, you can use the curing group to discard the standby state and improve efficiency
$str = ' Subject ';
Preg_match ('/(\w+):/', $STR, $arr);
Use the following method instead
Match ends when the first matching rule matches to T at the end of the text, enabling the second set of matching rules: The results are not found, so this time back to look, but \w will 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.