PHP Perl Regular Expression

Source: Internet
Author: User
Tags perl regular expression

/* string search is the main application of regular expressions. In PHP, the main function that can be used to match regular expressions in the Perl style is preg_match (). The function prototype is as follows
int preg_match (string pattern, string subject [, array matches [, int flags])
Search for Pattern in subject. $ Matches [0] will contain the text that matches the entire pattern.
$ matches [1] will contain the text that matches the child pattern in the first captured parentheses. So on
*/
$ text = "PHP is the Web scripting language of choice. ";
Print (" Search/"PHP/"/n "in text/" {$ text ");
If (preg_match ("/PHP/I", $ text )) {// the "I" after the pattern delimiter indicates that the big water writes are not distinguished during search
Print "-> find a match/n ";
}else {
Print "-> mode not found/n";
}

$ Text = "PHP is the website scripting language of choice .";
Print ("search for words/" Web/"/n" in text/"{$ text ");
If (preg_match ("// bweb/B/I", $ text) {// "/B" after the pattern delimiter indicates the word boundary, therefore, only the independent word "Web" can match
Print "-> find a match/n ";
} Else {
Print "-> mode not found/n ";
}

Print ("search for words/" Web/"/n" in text/"{$ text ");
If (preg_match ("/web/I", $ text )){
Print "-> find a match/n ";
} Else {
Print "-> mode not found/n ";
}

$ Text = "http://www.php.net/index.html ";
Print ("Get host name and domain name/n from ur/" {$ text ");
If (preg_match ("/^ (http :////)? ([^ //] +)/I ", $ text, $ matches) {// a subexpression enclosed in parentheses
$ Host = $ matches [2];
Print "-> Host Name: $ host/n ";

Preg_match ("/[^ /. //] + /. [^ /. //] + $/", $ host, $ matches); // obtain the following two segments from the host name
Print "-> Domain Name: {$ matches [0]}/N ";
}
?>

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.