Perl-based Regular Expression handler function _ Regular expression in PHP

Source: Internet
Author: User
Tags mixed modifier mysql in perl regular expression php and php and mysql

We have already learned the basic syntax for regular expressions, including delimiters, atoms, metacharacters, and schema modifiers. In fact, the regular expression handler function must be borrowed in the case in which it is intended to work. In this section, we will introduce Perl regular expression processing functions in PHP, including segmentation, matching, search, substitution, and so on processing operations, but also with the example explained, let's start.

As with regular expressions, regular expression processing functions cannot be used independently, and this must be combined to accomplish specific functions. As we have said before, Perl based regular expressions are faster than poxis regular expression handlers, so we'll only introduce Perl based regular expressions that start with preg. Note: Do not use regular expressions to handle strings when you can occasionally use String functions, because string processing functions are faster.

Let's look at some common regular expression handler functions.

1,preg_match () function.

function Preg_match () performs a regular expression match, which is defined as follows:

 
  

is actually searching for the matching pattern in the subject to be saved in the array matches. See the example:

<?php  
 
   $pattern = '/<b>.*?<\/b>/';  
   $string = ' <b>welcome</b> <b>to</b> <b>phpfuns</b>dsadsadas ';  
   if (Preg_match ($pattern, $string, $arr)) {  
     echo Regular expression <b>{$pattern}</b> and string <b>{$string}</b > Match Success <br> ";  
     Print_r ($arr);  
   else {  
     echo ' <font color= ' red ' > Regular expression {$pattern} and string {$string} match failed </font> ';  
   }  
 

2,preg_match_all () function.

The function Preg_match_all () function performs a global regular expression match, and its definition is consistent with the Preg_match () function, except that it matches all the results. Take a look at the example:

<?php  
   $pattern = '/<b>.*?<\/b>/';  
   $string = ' <b>welcome</b> <b>to</b> <b>phpfuns</b>dsadsadas ';  
   if (Preg_match_all ($pattern, $string, $arr)) {  
     echo Regular expression <b>{$pattern}</b> and string <b>{$string} </b> match Success <br> ";  
     Print_r ($arr);  
   else {  
     echo ' <font color= ' red ' > Regular expression {$pattern} and string {$string} match failed </font> ';  
   }  
 

Is still the example above (only the regular handler function is Preg_match_all ()), but the matching result array is different.

3, Preg_replace () function

The function preg_replace () performs a regular expression substitution, which is defined as follows:

Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]])

It is actually searching for the matching pattern in the subject and replacing it with replacement. Limit refers to the maximum number of times each mode replaces each subject. The default is-1 (infinite). If count is specified, it will be populated as the number of replacements completed.

Attention:

A, if subject is an array, preg_replace () returns an array, and in other cases returns a string.

B, if the match is found, the replacement subject is returned, and in other cases returns the subject that has not changed. Returns NULL if an error occurs.

C, the child mode can be applied to the parameter replacement, using either \ n or ${n}. (in the pattern of regular expressions we can only use \ n's form to get the matched child mode, remember!) )

D, if the pattern modifier e is used, the function can be parsed in the parameter replacement. (In other regular expression processing functions, the pattern modifier e is ignored!)

Consider the following comprehensive example:

<?php  
  $pattern = '/(PHP) | ( MySQL)/e ';  
  $string = ' The PHP and MySQL in this string are replaced with uppercase! ';  
  $result = Preg_replace ($pattern, ' Strtoupper ("${1}\2") ', $string,-1, $count);  
  echo $result. ' <br> ';  
  Echo $count;  

In the example above, we used the pattern modifier e, so that the Strtoupper () function can be parsed as a string, which is the function of the pattern modifier E! and the Parameters ${1} and \2 are sub mode 1 and sub mode 2 respectively. The role of the previous example is to replace the substring in the string $string with the schema PHP and MySQL in uppercase!

4,preg_split () function.

Preg_split executes a regular expression delimited string. The definition is as follows:

Array Preg_split (String $pattern, string $subject [, int $limit =-1 [, int $flags = 0]])

The fact is to split the subject according to pattern, returning the segmented array. Where limit will limit the number of substrings separated by up to limit, the last substring returned will contain all the rest. The limit value is-1, 0, or null to represent "unrestricted."

Let's look at an example:

<?php  
   $pattern = '/<p> (. *?) <\/p>/';  
   $string = ' The <p>php</p> and <p>mysql</p> in this string is split! ';  
   $result = Preg_split ($pattern, $string,-1, preg_split_delim_capture);  
   Print_r ($result);  
 


In the example above, we used the constant Preg_split_delim_capture setting that contains the child mode in the return result, if set to Preg_split_no_empty,preg_split () to return the separated non-empty part. If we remove the parentheses of the positive expressions in the example above, the results will no longer contain the two matching successful child schemas of PHP and MySQL.

Commonly used regular expression processing functions we have introduced, the example of this section may be difficult, but I hope that everyone is serious experiment and experience, the following regular Expression Application section, we will often use regular expression processing functions.

The above content is a small series to introduce PHP based on Perl regular expression processing functions, I hope to help you.

Related Article

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.