PHP regular expression learning notes

Copy codeThe code is as follows:
//Match text, which is easy to use occasionally, but be careful that the characters contain \ E  
$str = ‘[a-z]’; 
$str = preg_replace(‘/\G[a-z]\E/’, ”, $str); 
echo  $ str;  // Print empty, all replaced, probably preg_ Quote function  
Copy contents 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 codeThe code is as follows:
//It is only used for grouping parentheses, and the matching content will not be captured by variables. Sometimes it is necessary to improve the execution efficiency  
$str = ‘abc123abc’; 
preg_match(‘/abc(?:\d+)/’, $str, $arr); 
echo  $ arr[1];  // There is no $arr [1] except $arr [0], and \ 1 will not be given  

Copy codeThe code is as follows:
//Insert a good helper, forward and backward anchor search position, add a comma every 3 bits  
$str = ‘fdfad123456789fdfd’; 
$str = preg_replace(‘/(?<=\d)(?=(\d{3})+(?!\d))/’, ‘,’, $str); 
echo  $ str;  // Print   fdfad123,456,789fdfd  

//Match with minimal results
$str = 123456;
preg_match(‘/\d+/’, $str, $arr);
echo  $ arr[0];  // Yes, everyone knows it’s 123456
preg_match(‘/\d+?/’, $str, $arr);
echo  $ arr[0];  // This time it’s 1
//A more useful one can judge whether the previous ones have matched. For example, in the following example, you can ignore whether there are single quotation marks, double quotation marks or nothing to the right of the equal sign
$str = <<<HTML
<font size=12></font>
<font size=’13’></font>
<font size=”14″></font>
<font size=”15></font>
preg_match_all(‘/<font\s+size=([\'”]?)(\d+)\1[^>]*>/’, $str, $arr);
    [0] => 12
    [1] => 13
    [2] => 14
//Some pattern modifiers can also be placed in the expression
//Here, the color values in the style conforming to the XHTML specification are matched. The uppercase style is ignored, but the color in it can be case sensitive, which doesn’t matter
$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);
//You can also put the matching content inside and separate it with:, so you don’t have to write the end (? – I)
//preg_match_all(‘/style=([\'”]?)(?i:color:(\w+))\1/’, $str, $arr);
//Take another 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);  // Nothing matches
//Word retrieval, unfortunately, can only be used in English
$str = ‘I\’m a teacher’;
preg_match_all(‘/\b[a-z]+\b/i’, $str, $arr);
Copy contents to clipboard code:
//U modifier, match by Unicode
$str  = ‘ You you ‘;
$str  =  preg_ Replace (‘/ [you] /’,  ‘ you’,  $ str);
echo  $ str;  // It was disassembled and printed four times
//See the effect after adding the U modifier below. The modifier needs to be coded UTF-8, otherwise an error will be reported
//My text is GB2312, so I want to convert it 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
//X mode modifier, which can ignore whitespace and comments
$str = ‘test Test’;
preg_ match(‘/test   # Match only lowercase test / X ‘,  $ str,  $ arr);
Copy contents to clipboard code:
//Exclude look (<!…)   (?!…), ignore priority  *? +? ??  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);
//I couldn’t cope with it  $ str  = ‘ test  < B>test1<B>   test2</B>   test3</B>’;
//Just rewrite it   preg_ match_ all(‘/<B>(?:(?!<\/?B>).)*<\/B>/i’,  $ str,  $ arr);
//Complete the simplest UBB replacement according to the above
$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);
//If it is confirmed that there will be no matching result in the backtracking, the curing group can be used to abandon the standby state to improve efficiency
$str = ‘Subject’;
preg_match(‘/(\w+):/’, $str, $arr);
//Replace with the following method
//When the first set of matching rules matches to the end t of the text, the matching ends. Enable the second set of matching rules: the result will not be found, so the search will be carried out back at this time, but \ w will not contain:, so you can give up directly,
preg_match(‘/(?>\w+):/’, $str, $arr);

Recommended Today

Paper case CMS 3.3.6 release upgrade. Net 5

.Net 5 Microsoft’s. Net 5 has been released and we adapted it for the first time. The biggest change in the new version is to upgrade to Microsoft’s latest. Net 5, and the performance has been significantly improved. So please update your running environment before upgrading to 3.3.6. Upgrade Guide: Convenient update Paper shell CMS […]