PHP Introductory tutorial on regular Expressions Basic usage examples (regular matching, search, segmentation, etc.) _php skills

Source: Internet
Author: User

This example describes the basic usage of PHP regular expressions. Share to everyone for your reference, specific as follows:

demo1.php

<?php
 //Try to write the first regular expression//
 first argument, representing the pattern (that is, comparing it to the string, matching)
 //The second parameter, representing the string
 //If the entire function pattern matches the string comparison, Then return True (TRUE) 1, otherwise return False (FALSE) 0
 //What is the match, is one of the comparison past
 //According to what rules?? Follow the pattern to match
 //As long as the pattern is all through, then the through
 //match and equality are two concepts
 //Because the current pattern is a whole, PHP
 //So, strings as long as there are linked PHP three characters, regardless of the string can match
 $mode = '/php/';//rule mode
 $string = ' fsdfsdfsdphpfsdfs ';//String
 //echo preg_match ($mode, $string);
 if (Preg_match ($mode, $string)) {
 echo ' matches ';
 } else{
 Echo ' does not match ';
 }
 echo preg_match ('/php/', ' php ');
if (' a ' = = = ' A ') {
//echo ' equals ';
}else {
//echo ' not equal ';
}
?>

demo2.php

 <?php//What is the preamble, the leading character of the preceding character, the//+ number, is H//h+, which means at least one of the H//+ numbers means//$mode = '/ph+p /'; Rule pattern//h* means that 0 or more H//* numbers may be 0, but the leading characters cannot be changed, and they do not match//$mode = '/ph*p/'; Rule mode//h? 0 or one H//? Multiple leading characters no//$mode = '/ph?p/'; Rule mode//. meaning 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/'; Rule Mode//h{3} indicates that the leading must be 3,//$mode = '/ph{3}p/'; Rule Mode//h{3,5} indicates that the leading must be 3 to 5//$mode = '/ph{3,5}p/'; Rule pattern//h{3,} indicates that a leading at least 3 h//$mode = '/ph{3,}p/'; Regular mode//$ symbol, usually added to the string tail of the pattern//indicates starting from the tail//$mode = '/php$/'; Rule pattern//^, matching from the beginning//$mode = '/^php/'; Rule mode//^ and $ together, basically do not use regular, directly with the = = number can//$mode = '/^php$/'; Rule pattern//|
 is a condition selector, which can be matched if one is available. $mode = '/php|asp/'; Rule mode//() $mode = '/(this) (IS) (php|asp)/'; Rule mode $string = ' This is php ';
 String//echo preg_match ($mode, $string);
 if (Preg_match ($mode, $string)) {echo ' match ';
 }else{echo ' mismatch '; }?> 

demo3.php

<?php
 //unary
 //[] bracket, is a regular in the grammar
 //inside the expression of any one can be
 //[a-z] represents 26 letters, any one
 //$mode = '/[a-z]/'; Rule pattern
 //[0-9] represents 0-9, any one/
 /$mode = '/[0-9]/';//rule pattern
 //[ASV] One
 //$mode = '/[asv]/';//Rule Pattern
 [A-za-z0-9_] represents any one//$mode = '/[a-za-z0-9_]/' In one of the matches [a-za-z0-9_];//
 rule pattern
 //not match at the beginning of ^, but in brackets
 //[^] matches strings that match except ABC
 //$mode = '/[^abc]/';//Rule mode//
 to make it more convenient for people to use, \w represents [a-za-z0-9_]
 //$mode = '/[\w]/'; Regular mode
 //\w is exactly the opposite of \w
 //$mode = '/[\w]/';//rule pattern
 //\d and [0-9]
 //$mode = '/\d/';//rule pattern
 //\d and [ ^0-9] Same
 //$mode = '/\d/';//rule pattern
 //\s match any white space character
 //$mode = '/\s/';//Rule mode
 //\s does not contain whitespace characters
 //$mode = '/\s/'; The rule pattern//\b indicates that the boundary of the
 word is reached, and if it does not match, the boundary can be interpreted as a space
 //$mode = '/php\b/';//The rule pattern
 //\b indicates 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 a special character, you must add \
 $mode = '/p\+p/';//rule mode
 $string = ' p+p ';//String
 if (Preg_match ($mode, $string)) {
 echo ' matches ';
 } else{
 Echo ' does not match '
 ;
>

demo4.php

 the <?php//modifier
 /modifier is placed outside the//
 //i to indicate case-insensitive
//$mode = '/php/i ';//Rule mode
//$string = ' php ';//String
 M indicates that when the match ends, if the line is encountered, it should also be recognized as ending
//$mode = '/php$/m ';//Rule mode
//$string = "This is a php\n, is goods";//String
 //x Indicates that whitespace characters/$mode = '/php/x ' in the rule pattern are ignored///////
The rule mode
//$string = ' ph p ';//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 ' match ';
 } else{
 Echo ' does not match '
 ;
>

demo5.php

<?php
 //Search array matches the string
 //preg_grep () function
 $language = array (' PHP ', ' asp ', ' jsp ', ' Python ', ' Ruby ');
 Find out what the most popular 3p language
 //three languages match in common, and end up with P
 //end matching p/p$/
 //$mode = '/p$/';
 Print out the language at the beginning of P
 $mode = '/^p/';
 Python python 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///
 by split method group
 $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 ' e-mail is illegal ';
>

demo8.php

<?php
 //Match Global regular
 //To put all the matching results of a 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
 //defined then
 echo preg_quote (' PHP is $ ');//php is \$150
?>

demo10.php

<?php
 //Search for matching results, then replace
 ///First argument, save regular mode
 //second parameter, put the replaced string
 //second argument, character
 //will be the third parameter of the string of PHP5, Php6 replaced by Onestopweb
 Echo preg_replace ('/php[1-6]/', ' onestopweb ', ' This is a php5,this ');
 This is a onestopweb,this is a onestopweb
?>

demo11.php

<?php
 //greed and group capture cases, UBB
 //I'm going to change this [b][/b] to <strong></strong>
 //Pay attention to a problem, at this time of the [] bracket, Is the parentheses of the string, not the syntax [a-z]//
 . Means to match any character one, plus a * number 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
 //There are only 1 groups, \1
 //First question, the first [b] and the last [/b] match
 //solve the greedy match.
 $mode = '/(\[b\]) (. *) (\[\/b\])/u ';//u prohibition of greed
 $replace = ' <strong>\2</strong> ';
 $string = ' is a [B]php5[/b],this is a [B]PHP6 [/b] ';
 echo $string;
 Echo preg_replace ($mode, $replace, $string);
? >

demo12.php

<?php/////
 If there is no [] symbol, it means to satisfy
 Print_r (preg_split ('/[.@]/', ' oneStopWeb@163.com ') at the same time;
 Array ([0] => onestopweb [1] => 163 [2] => com)
?>

PS: Here again for you to provide 2 very convenient regular expression tools for your reference to use:

JavaScript Regular expression online test tool:
Http://tools.jb51.net/regex/javascript

Regular expression online generation tool:
Http://tools.jb51.net/regex/create_reg

For more information on PHP related content readers can view the site topics: PHP Regular Expression Usage summary, PHP array operation technique Encyclopedia, PHP Basic Grammar Getting Started tutorial, PHP operation and operator Usage Summary, PHP object-oriented Program design Introductory Course, " PHP Network Programming skills Summary, "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.