PHP Regular Expression Concepts

Source: Internet
Author: User

Delimiter

Usually use "/" as the delimiter to start and end, you can also use "#". Usually when there are many "/" characters in the string, use "#" as the delimiter, because the character needs to be escaped, such as a URI.
  <?php  /**   * delimiter   * **/ span> $regex   =   '/^http:\/\/([\w.]  +) \/([\w]+)/([\w]+)/([\w]+)/([\w]+]/([\w]+] \.html$/i ' ;  $str   =    ' http://php1234.cn/a/functions/2016/0905/50.html ' ;  $matches   =   Array   ();  if  ( preg_match  ( $regex ,  $str ,  $matches ))  { var_dump  ( $matches );  $regex   =   ' #^http:\/\/([\w.]  +) \/([\w]+)/([\w]+)/([\w]+)/([\w]+]/([\w]+) \.html$ #i ' ;  if  ( preg_match  ( $regex ,  $str ,  $matches ))  { var_dump  ( $matches ); } ?  
Output: Array (7) {  [0]=>  string (http://php1234.cn/a/functions/2016/0905/50.html) "  [1]=>  " String (Ten) "php1234.cn"  [2]=>  string (1) "A"  [3]=>  string (9) "Functions"  [4]=>  String (4) "  [5]=>  string (4)" 0905 "  [6]=>  string (2)" "}array (7) {  [0]=>  string ("http://php1234.cn/a/functions/2016/0905/50.html"  ) [1]=>  String (Ten) "php1234.cn"  [2]=>  string (1) "A"  [3]=>  string (9) "Functions"  [4]=>  String (4) "  [5]=> string (  4)" 0905 "  [6]=>  string (2)" 50 "}
"Modifier"
The symbol used to change the behavior of the regular expression, the last of the expressions in the above example,/I is a modifier to ignore the case, and a more commonly used is "X", which is used to denote ignoring spaces.
"Character Field"
The part that expands with square brackets is the word value field, as in the previous example: [\w].
"Qualifier"
Symbols such as [\w]{3,5} or [\w]*] or [\w]+ these [\w] represent qualifiers. {3,5} represents 3 to 5 characters. {3,} more than 3 characters, {, 5} up to 5. {3} three characters. * Represents 0 to more. + represents 1 to more.
"Off character"
^: In the word value field (e.g. [^\w]), the negation (not including the meaning) means "reverse selection".  before the expression, it begins with the current character. (/^n/i, which means start with N). Note: We often call "\" called "Skip character". Used to escape some special symbols, such as ".", "/"
"Wildcard character"
Determine the presence or absence of certain characters in a string! Format: Forward pre-check: (? =) corresponds to (?!) Negative meaning reverse pre-check: (? <=) corresponds to the (? <!) To express a negative meaning
<?php  /**   * Wildcard characters   * **/  $regex = '/(? <=c) d (? =e)/';  /* d followed by C, and D followed by e*/  $str = ' abcdefgk ';  $matches = Array ();  if (Preg_match ($regex, $str, $matches)) {      Var_dump ($matches);  }  $regex = '/(? <!c) d (?! e)/';//negative meaning  $str = ' abcdefgdk ';  if (Preg_match ($regex, $str, $matches)) {      Var_dump ($matches);  }?>
Output: Array (1) {  [0]=>  string (1) "D"}array (1) {  [0]=>  string (1) "D"}
"Lazy Match"
Format: Qualifier? Principle: "?" : The smallest data is used if there is a qualifier in front of it. such as "*" will take 0, and "+" will take 1, if it is {3,5} will take 3.
<?php  /**   * Lazy Matching   * **/  $regex = '/hel*/i ';$str = ' hellllllllllllllll ';if(Preg_match($regex,$str,$matches)){Var_dump($matches); }$regex = '/hel*?/i ';$str = ' hellllllllllllllll ';if(Preg_match($regex,$str,$matches)){Var_dump($matches); }$regex = '/hel+?/i ';$str = ' hellllllllllllllll ';if(Preg_match($regex,$str,$matches)){Var_dump($matches); }$regex = '/hel{5,8}?/i ';$str = ' hellllllllllllllll ';if(Preg_match($regex,$str,$matches)){Var_dump($matches); }?>
Output: Array (1) {  [0]=>  string (+) "Hellllllllllllllll"}array (1) {  [0]=>  string (2) "he"}array ( 1) {  [0]=>  string (3) "HeL"}array (1) {  [0]=>  string (7) "Helllll"}
*: 0 to multiple +:1 can also be written as {1,}? : 0 or 1 times.  

PHP Regular Expression Concepts

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.