PHP Introductory Tutorial Regular expression basic usage examples (regular matching, search, segmentation, etc.)

Source: Internet
Author: User
The basic usage of PHP regular expressions is described in this article. Share to everyone for your reference, as follows:

demo1.php

<?php//Try to write the first regular expression//the first parameter, representing the pattern (that is, the string to compare, match)///The second argument, the string///if the entire function pattern and string comparison match, then return True (TRUE) 1, otherwise return False (FALSE) 0// What is a match, is a comparison of the past//according to what rules?? Follow the pattern to match//as long as the pattern all passes, then through//matching and equality is two concepts//Because the current mode is a whole, PHP//So, string as long as there is a connection PHP three characters, regardless of the string can match $mode = '/php/'; Rule pattern $string = ' Fsdfsdfsdphpfsdfs '; String//echo preg_match ($mode, $string); if (Preg_match ($mode, $string)) {echo ' matches ';} else{echo ' mismatch ';}//echo preg_match ('/php/', ' php ');/if (' a ' = = ' a ') {//echo ' equal ';//}else {//echo ' not equal ';//}?>

demo2.php

<?php//What is called Preamble, preceded by a character//+ the leading character of the number is H//h+ meaning that it contains at least one H//+ number meaning//$mode = '/ph+p/'; Rule mode//h* means that 0 or more H//* can be 0, but the leading character cannot be changed, and it does not match//$mode = '/ph*p/'; Rule mode//h? means 0 or one H//? Multiple leading characters do not//$mode = '/ph?p/'; The rule pattern//. means that any one character, so, two points, is any two characters//$mode = '/p.p/'; The rule pattern//.* represents a leading arbitrary character, and 0 or more//$mode = '/p.*p/'; The rule pattern//h{3} indicates that the preamble must be 3,//$mode = '/ph{3}p/'; The rule pattern//h{3,5} indicates that the preamble must be between 3 and 5/$mode = '/ph{3,5}p/'; The rule pattern//h{3,} indicates a leading at least 3 h//$mode = '/ph{3,}p/'; The rule pattern//$ symbol, usually added to the pattern of the string tail//expression starting from the tail match//$mode = '/php$/'; The rule pattern//^ indicates that a match is made from the beginning//$mode = '/^php/'; Rule mode//^ and $ together, basically do not use the regular, directly with the = = number can//$mode = '/^php$/'; Rule Mode//| is a conditional selector and can be matched if one of them is available. $mode = '/php|asp/'; Rule pattern//() $mode = '/(this) (IS) (php|asp)/'; Rule mode $string = ' This is php '; String//echo preg_match ($mode, $string); if (Preg_match ($mode, $string)) {echo ' matches ';} else{echo ' mismatch ';}? >

demo3.php

<?php//meta-//[] in parentheses, is a regular syntax//inside something that represents any one can//[a-z] represents 26 letters, any one of them//$mode = '/[a-z]/'; Rule pattern//[0-9] represents 0-9, any one of them//$mode = '/[0-9]/'; Rule mode//[ASV] One//$mode = '/[asv]/'; Rule pattern//[a-za-z0-9_] indicates that one of the matches [a-za-z0-9_] is either//$mode = '/[a-za-z0-9_]/'; The rule pattern//is not at the beginning of the ^ that matches the beginning of the line, but in brackets the ^//[^] indicates that matches except ABC can match the string//$mode = '/[^abc]/'; Rule mode//in order to make people use more convenient, \w represents [a-za-z0-9_]//$mode = '/[\w]/'; The rule pattern//\w exactly and \w opposite//$mode = '/[\w]/'; Rule pattern//\d and [0-9]//$mode = '/\d/'; The rule pattern//\d and [^0-9] the same//$mode = '/\d/'; The rule pattern//\s matches any whitespace character//$mode = '/\s/'; The rule pattern//\s does not contain white space characters//$mode = '/\s/'; The rule pattern//\b means that the boundary of the word is reached, and if it does not match, the boundary can be understood as a space//$mode = '/php\b/'; The rule pattern//\b means that the boundary is not reached//$mode = '/php\b/'; Rule mode//because the + number is a special character in the regular//if you want to match Special characters, you must add \ $mode = '/p\+p/'; Rule pattern $string = ' p+p '; String if (Preg_match ($mode, $string)) {echo ' matches ';} else{echo ' mismatch ';}? >

demo4.php

<?php//modifier//modifier is placed outside of////i means case insensitive//$mode = '/php/i '; Rule mode//$string = ' PHP '; The string//m represents the end of the match, and if a newline is encountered, it should also be acknowledged as ending//$mode = '/php$/m '; Rule mode//$string = "This is a php\n, is goods"; The string//x indicates that whitespace characters in the rule pattern are ignored//$mode = '/php/x '; Rule mode//$string = ' ph P '; The string//a indicates that a match must be started from scratch $mode = '/php/a '; Rule pattern $string = ' PHPFSDFD '; String if (Preg_match ($mode, $string)) {echo ' matches ';} else{echo ' mismatch ';}? >

demo5.php

The <?php//search array matches the string//preg_grep () function $language = array (' PHP ', ' asp ', ' jsp ', ' Python ', ' Ruby '); Find out the most popular 3p languages//What are the similarities between the two languages, and the final end are P//end match p/p$///$mode = '/p$/'; Print out the language at the beginning of P $mode = '/^p/'; Python boa language, in foreign countries have replaced PHP rumors Print_r (Preg_grep ($mode, $language)); >

demo6.php

<?php//Search mode, the last return is true or FALSE, 1,10 echo preg_match ('/php[1-6]/', ' php5 ');? >

demo7.php

<?php//e-mail small case//grouped by split method $mode = '/^ ([\w\.] {2,255}) @ ([\w\-]{1,255}). ([a-z]{2,4}) $/';//mode cannot be missing $string = ' oneStopWeb@163.com '; if (Preg_match ($mode, $string)) {echo ' e-mail legal ';} else{Echo ' email is illegal ';}? >

demo8.php

<?php//Match global regular//put all matching results of the string into an array variable preg_match_all ('/php[1-5]/', ' php5fsdfsdphp6fsdfsdfphp4 ', $out); Print_r ($out);//array ([0] = = Array ([0] = php5 [1] = PHP4)) echo $out [0][0];//php5?>

demo9.php

<?php//definite definition of echo preg_quote (' PHP is $ $ '); PHP is \$150?>

demo10.php

<?php//Search for matching results, then replace//The first parameter, save the regular mode//second parameter, put the replacement string//second argument, the character//the string of the third argument is replaced by the php5,php6 of the Onestopweb Echo preg_ Replace ('/php[1-6]/', ' onestopweb ', ' This was a php5,this is a php6 '); A onestopweb,this is a onestopweb?>

demo11.php

<?php//greed and packet acquisition cases, UBB//I'm going to replace this [b][/b] with <strong></strong>//attention to a question, this time the [] bracket is the parenthesis of the string, not the syntax [a-z]//. means match any character one, plus a * sign to match 0 or more//with parentheses divided into three groups, then the first group is \1, the second group is \2, the third group is \3//Currently only 1 groups, \1//The first problem, the first [b] and the last [/b] Match//resolve the greedy match. $mode = '/(\[b\]) (. *) (\[\/b\])/U ';//u no greed $replace = ' <strong>\2</strong> '; $string = ' This was a [B]php5[/b],this is a [B]PHP6 [/b] '; Echo $string; Echo preg_replace ($mode, $replace, $string);? >

demo12.php

<?php//Use regular expression to split//if there is no [] symbol, it means to satisfy both print_r (Preg_split ('/[.@]/', ' oneStopWeb@163.com ')); Array ([0] = onestopweb [1] = 163 [2] = = com)?>

I hope this article is helpful to you in PHP programming.


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.