On regular Expressions (Regular Expression) _ Regular expressions

Source: Internet
Author: User
Tags numeric perl regular expression

One, what is regular expression?

Simply put: Regular expression (Regular Expression) is a language that handles string matching;

A regular expression describes a pattern of string matching that can be used to check whether a string contains a seed string, and to "fetch" or "replace" the substring that matches it.

Second, the application of regular expressions

Regular expressions in the actual development process is very practical, can quickly solve some complex string processing problems, I have a regular expression of the application to do some simple classification:

The first type: data validation
For example, if you want to verify whether a string is the correct email,telphone,ip and so on, then it is very convenient to use regular expressions.

The second type: Content lookup
For example, if you want to crawl a picture of a Web page, then you definitely need to find tags, which can be accurately matched with regular expressions.

The third type: content substitution
For example, if you want to hide the mobile phone number between four digits into this mode, 123****4567, then the use of regular expressions will be very convenient.

Iii. what is the regular expression

Here's a quick introduction to regular expressions:

1. Several important concepts of regular expressions
• Subexpression: In a regular expression, if you use the content enclosed in "()", it is called a "subexpression"
• Capture: The result of a subexpression match is placed in a buffer by the system, which we call "capture"
• Reverse Reference: we use "\ n" where n is a number representing the contents of a previous buffer, which we refer to as "reverse reference"
2. Quantity qualifier
x+ said: 1 or more
X* said: 0 or more
x? said: 0 or 1
x{n} says: N A
X{n,} says: at least N
X{n,m} said: N to M, greedy principle, will match as many as possible; If you add one to the back? , the principle of non-greed
Note: X indicates the character to look for

3, character qualifier
\d: Matches a numeric character, [0-9]
\d: Match a non-numeric character, [^0-9]
\w: Matches the word characters including underscores, [0-9a-za-z_]
\w: Matches any non word character, [^0-9a-za-z_]
\s: Matches any white space character, space, carriage return, tab
\s: Matches any non-white-space character
•. Represents: matches any single character
In addition, there are several following:

Range characters: [A-z], [A-z], [0-9], [0-9a-z], [0-9a-za-z]
Any character: [ABCD], [1234]
Characters not included: [^a-z], [^0-9], [^ABCD]

4, locator character
^: Beginning identification
$: End identification
\b Says: Word boundaries
\b Says: non-word boundaries
5, escape character
\ used to match certain special characters
6, select the matching character
| can match multiple rules
7. Special usage
• (? =): Forward check: Matches the string ending with the specified content
• (?!): Negative check: Matches a string that is not the end of the specified content
• (?:): Do not put the selection of matching characters into the buffer

Four, regular expressions in the use of JavaScript under the method

There are two ways to use regular expressions in javascript:

The first method: using the RegExp class
The methods provided are:

Test (str): String that matches whether there is a match pattern in the string, returns True/false
exec (str): Returns the string to which the matching pattern matches, if any, returns the corresponding string, none, and returns null;
If there are child expressions in the regular expression, the Exec method is used

Returns: result[0] = match result, result[1] = Match of subexpression 1 ...

The second approach is to use the String class
The methods provided are:

search: Returns the position of the string that matches the pattern, if not, returns-1
match: Returns the string to which the matching pattern matches, if any, returns an array, none, returns null
Replace: Replace the string matched to the matching pattern
split: String delimited by string matching pattern, returning array

Five, regular expression in PHP under the use of the method

There are two functions in PHP that use regular expressions:

The first is: perl Regular expression functions
The methods provided are:

Preg_grep--Returns the array cells that match the pattern
Preg_match_all--Global regular expression matching
Preg_match--a regular expression match
Preg_quote--escape Regular expression characters
Preg_replace_callback--using callback functions to perform search and replace of regular expressions
Preg_replace--Performs search and replace of regular expressions
Preg_split--splitting a string with a regular expression
The second is: POSIX regular expression functions
The methods provided are:

Ereg_replace--replacing regular expressions
Ereg--Regular expression matching
eregi_replace--case-insensitive substitution of regular expressions
Eregi--case-insensitive regular expression matching
Split--splitting a string into an array with a regular expression
Spliti--Split a string into an array with a regular expression case-insensitive
Sql_regcase--produces a regular expression for a case-insensitive match

Vi. Summary

A regular expression is a tool for us to implement a function:

1. Strong function
The different combinations of qualifiers in regular expressions implement different functions, and sometimes it takes a programmer's ability to implement a complex function that requires a long regular expression to be written and how to match it accurately.

2. Simple and convenient
Usually we are in the search for string content, can only do a certain string lookup, but regular expressions can help us to do Fuzzy Lookup, faster and more convenient, just need a regular expression string.

3, various languages are basically supported
Regular expressions are currently supported in mainstream languages such as Java, PHP, Javascript, C #, and C + +.

4, the study is very simple, the application is very advanced
It is very easy to learn regular expressions, but how to write efficient, accurate regular expressions in actual development still requires a long time to try and accumulate.

If you want to get started quickly, review this article:

Regular expression 30-minute introductory tutorial

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.