Chapter 9 Regular Expressions

Source: Internet
Author: User

Note: This article is a video tutorial from Li Yanhui PHP. This article is intended for communication purposes only and cannot be used for commercial purposes. Otherwise, the consequences will be borne by you.

Learning points:
1. Regular expression syntax (Perl style)
2. Elements in Regular Expressions
3. Perl-style functions

 

When processing strings, many complicated strings cannot be processed cleanly using common string processing functions. Ratio
For example, you may need to verify that an Email address is valid. Therefore, you need to check many rules that are not easy to check. This is
Is the use of regular expressions. Regular Expressions are powerful and concise character groups, which can contain a large number of tokens.
It is particularly worth mentioning that regular expressions are quite short.

I. regular expression syntax (Perl style)

Perl has always been considered one of the greatest parsing languages, and it provides a comprehensive regular expression, even the most
You can use this regular expression to search for and replace complex string modes. PHP developers realize that
It re-invented regular expressions. It is better to allow PHP users to directly use the prestigious Perl Regular Expression Language, namely, Perl
Style functions.
Mode rule:/php/Add two slashes before and after the string.
Matching function: The preg_match () function searches strings for modes. If yes, true is returned. Otherwise, false is returned.

<?('/php/','php'?>

Ii. Elements in Regular Expressions

Regular Expressions contain three elements: quantifiers, metacharacters, and modifiers.

Quantifiers

Metacharacters

Modifier

 

 

Iii. Perl-style functions

PHP provides seven functions for searching strings using Perl-Compatible Regular expressions, including: preg_grep (),
Preg_match (), preg_match_all (), preg_auote (), preg_replace (), preg_replace_callback (), and
Preg_split ().
Search string: The preg_grep () function searches all elements in the array and returns all elements that match a certain pattern.
Element array.

<? = ('php','asp','jsp','python','ruby'(('/p$/',?>

Search Mode: The preg_match () function searches strings in the search mode. If yes, true is returned. Otherwise, false is returned.

<? ('/php[1-6]/','php5'?>

Small case of email verification (group application)

<? = '/([\w\.\_]{2,10})@(\w{1,}).([a-z]{2,4})/' = 'yc60.com@gmail.com' (,?>

All occurrences of the matching mode: The preg_match_all () function matches all occurrences of the pattern in the string, and then
Put all matched values into the array.

<?('/php[1-6]/','php5sdfphp4sdflljkphp3sdlfjphp2',(?>

Define a special regular expression: preg_quote (). Each word has a special meaning for the regular expression syntax.
Insert a backslash before. These special characters include: $ ^ * () + ={} [] |\:<>.

<? ('Php price: $150 '?>

All occurrences of the replacement mode: The preg_replace () function searches for all matches and replaces them with the desired string to return
.

<? ('/php[1-6]/','python','This is a php5,This is a php4'?>

Ubb small case: greedy problem + grouping ()

<? = '/\[b\](.*)\[\/b\]/U' = '<strong>\1</strong>' = 'This is a [b]php5[/b],This is a [b]php4[/b]' (,,?>

Strings are divided into different elements in case-insensitive mode: preg_split () is used to separate different elements.

<?(('/[\.@]/','yc60.com@gmail.com'?>

Note: currently, seven functions are provided for PHP to search strings using POSIX regular expressions, including ereg (),
Ereg_replace (), eregi (), eregi_replace (), split (), spliti (), and SQL _regcase ().
PS: The style is basically the same as that of Perl. You can refer to the Manual to learn it yourself.

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.