Regular Expressions (full version)

Source: Internet
Author: User

Regular expression original address: http://www.jb51.net/tools/zhengze.html Regular Expression 30-minute introductory tutorial

Version: v2.33 (2013-1-10) Deerchao reprint please indicate the source

Directory

Skip Directory

    1. Objective of this article
    2. How to use this tutorial
    3. What exactly is a regular expression?
    4. Entry
    5. To test the regular expression
    6. Metacharacters
    7. Character escapes
    8. Repeat
    9. Character class
    10. Branching conditions
    11. Anti-righteousness
    12. Group
    13. Back to reference
    14. 0 Wide Assertion
    15. Negative 0 Wide Assertion
    16. Comments
    17. Greed and laziness
    18. Processing options
    19. Balance Group/recursive matching
    20. There's something else that hasn't been mentioned.
    21. Contact author
    22. Resources on the Internet and references in this paper
    23. More Records
Objective of this article

Within 30 minutes let you know what the regular expression is and have a basic understanding of it so that you can use it in your own program or Web page.

How to use this tutorial

The most important thing is--give me 30 minutes, if you don't have experience with regular expressions, please don't try to get started in 30 seconds--unless you're Superman:)

Don't be intimidated by the complex expressions below, as long as you follow me one step at a pace, you will find that regular expressions are not as difficult as you might think. Of course, if you have read this tutorial and found that you understand a lot, but almost nothing to remember, it is also very normal-I think that people who have not contacted the regular expression after reading this tutorial, can remember the grammar mentioned above 80% probability of zero. Here is just to let you understand the basic principle, you need to practice more, use more, to master the regular expression.

In addition to being an introductory tutorial, this article attempts to be a regular expression syntax reference manual that you can use in your daily work. As far as the author's experience is concerned, the goal is well done--you see, I haven't been able to write everything down, have I?

Clear Rich Text formatting conventions: Professional terms meta-character/syntax format a part of a regular expression (for parsing) that matches a source string to a regular expression or part thereof

Hide Edge Note There are some comments to the right of this article, mainly to provide some relevant information, or to explain some basic concepts to readers without a programmer's background, which can usually be ignored.

What exactly is a regular expression?

Characters are the most basic unit of computer software processing text, which may be letters, numbers, punctuation, spaces, line breaks, kanji, etc. A string is a sequence of 0 or more characters. Text is the literal, string. To say that a string matches a regular expression, usually refers to a part (or parts of it) in the string that satisfies the condition given by an expression.

When writing a program or Web page that handles strings, there is often a need to find strings that match certain complex rules. Regular expressions are the tools used to describe these rules. In other words, the regular expression is the code that records the text rule.

It is possible that you have used the wildcard character (wildcard) for file lookup under Windows/dos, which is * and?. If you want to find all the Word documents in a directory, you will search for *.doc. Here, * will be interpreted as an arbitrary string. Like wildcard characters, regular expressions are also tools for text matching, but rather than wildcards, it can describe your needs more precisely--and, of course, the cost is more complex--you can write a regular expression that looks for all 0, followed by 2-3 numbers, then a hyphen "-", The last is a 7-or 8-digit string (like 010-12345678 or 0376-7654321).

Entry

The best way to learn a regular expression is to start with an example and then modify the example yourself to experiment. Here are a few simple examples, and they are described in detail.

Suppose you look for hi in an English novel, you can use the regular expression hi.

This is almost the simplest regular expression, it can exactly match such a string: Two characters, the previous character is H, the latter is I. Typically, a tool that handles regular expressions provides an option to ignore the case, and if this option is selected, it can match any of the four cases of Hi,hi,hi,hi.

Unfortunately, many words contain the two consecutive characters of Hi, such as Him,history,high and so on. With Hi to find, the side of the hi will be found. If we want to find the word hi exactly, we should use \bhi\b.

\b is a special code prescribed by regular expressions (well, some people call it metacharacters, metacharacter), which represents the beginning or end of a word, that is, the boundary of a word. Although English words are usually delimited by spaces, punctuation marks, or line breaks, \b does not match any of these word-delimited characters, it only matches one position .

If a more precise argument is needed, \b matches such a position: its previous character and the next one are not all (one is, one is not or does not exist) \w.

If you're looking for a hi, not far behind, follow a Lucy, you should use \bhi\b.*\blucy\b.

Here,. is another meta-character that matches any character except the line break. * is also a meta-character, but it does not represent a character, nor a position, but a quantity-it specifies that the contents of the front can be reused any number of times to match the entire expression. Therefore,. * Together means any number of characters that do not contain a newline. Now the meaning of \bhi\b.*\blucy\b is obvious: first a word hi, then any arbitrary character (but not a newline), and finally the word Lucy.

The newline character is ' \ n ' and the ASCII encoding is 10 (hexadecimal 0x0a).

If you use a different meta-character at the same time, we can construct a more powerful regular expression. For example, the following:

0\d\d-\d\d\d\d\d\d\d\d matches such a string: Starting with 0, then two digits, then a hyphen "-", and finally 8 digits (that is, China's phone number.) Of course, this example only matches the case where the area code is 3 bits).

The \d here is a new meta-character that matches one digit (0, or 1, or 2, or ...). -Not a meta-character, only matches itself-a hyphen (or a minus sign, or a middle line, or whatever you call it).

To avoid so many annoying repetitions, we can also write this expression: 0\d{2}-\d{8}. here {2} ({8}) behind \d means that the front \d must match 2 times consecutively (8 times).

To test the regular expression

Other test tools available:

    • Regexbuddy
    • JavaScript Regular expression online test tool

If you don't find the regular expression difficult to read or write, either you are a genius, or you are not a man of the Earth. The syntax of regular expressions is a headache, even for those who often use it. Because it is difficult to read and write, error-prone, it is necessary to find a tool to test the regular expression.

Some details of regular expressions in different environments are not the same, this tutorial describes the behavior of regular expressions under Microsoft. Net Framework 4.0, so I recommend that I write to you. NET tool Regular expression tester. Please refer to the instructions on this page to install and run the software.

The following is the Regex Tester Runtime:

Metacharacters

Now you know a few useful metacharacters, such as \b,., * and \d. There are more metacharacters in regular expressions, such as \s matches any whitespace, including spaces, tabs (tab), line breaks, Chinese full-width spaces, and so on. \w matches letters or numbers or underscores or kanji.

The special handling of Chinese/kanji is by. NET provides the regular expression engine support, in other circumstances, see the relevant documents.

Let's take a look at more examples below:

\ba\w*\b matches a word that begins with the letter A-first at the beginning of a word (\b), then the letter A, then any number of letters or numbers (\w*), and finally the end of the word (\b).

OK, now let's talk about what the word in the regular expression means: it's not less than a continuous \w. Yes, it does not really matter with the thousands of things that you have to memorize when learning English:)

\d+ matches 1 or more consecutive digits. Here the + is and * similar to the meta-character, the difference is * match repeat any time (may be 0 times), and + match repeat 1 or more times.

\b\w{6}\b matches exactly 6 characters of a word.

Table 1. Commonly used meta-characters
Code Description
. Match any character other than line break
\w Match letters or numbers or underscores or kanji
\s Match any of the whitespace characters
\d Match numbers
\b Match the beginning or end of a word
^ Match the start of a string
$ Match the end of a string

The regular expression engine typically provides a "test whether a specified string matches a regular expression" method, such as the Regexp.test () method in JavaScript or. NET Regex.IsMatch () method. The match here refers to the part of the string that matches the expression rule. If you do not use ^ and $, for \d{5,12}, using this method can only guarantee that the string contains 5 to 12 consecutive digits, instead of the entire string is 5 to 12 digits.

The meta-character ^ (and the number 6 on the same keyed symbol) and $ all match a position, which is a bit similar to \b. ^ matches the end of the string you want to use to find the beginning of the match. These two codes are useful when validating input, such as a Web site that requires you to fill in a 5-bit to 12-digit QQ number, which you can use: ^\d{5,12}$.

The {5,12} here is similar to the {2} described earlier, except that {2} matches can only be repeated 2 times, and {5,12} is repeated no less than 5 times, not more than 12 times, otherwise it does not match.

Because ^ and $ are used, the entire string entered is used to match the \d{5,12}, which means that the entire input must be 5 to 12 digits, so if the QQ number entered matches the regular expression, then it will meet the requirements.

Similar to ignoring case options, some regular expression processing tools have an option to handle multiple rows. If this option is selected, the meaning of ^ and $ becomes the beginning and end of the matching line.

Character escapes

If you want to find the meta-character itself, such as when you look up, or *, there's a problem: You can't specify them, because they'll be interpreted as something else. Then you have to use \ To cancel the special meaning of these characters. Therefore, you should use \. and \*. Of course, to find \ itself, you also have to use \ \.

For example: Deerchao\.net matches deerchao.net,c:\\windows matching C:\Windows.

Repeat

You've seen the previous *,+,{2},{5,12} and these are several ways to match duplicates. The following are all qualifiers in a regular expression (a specified number of codes, such as *,{5,12}, and so on):

Table 2. Common Qualifiers
Code/Syntax Description
* Repeat 0 or more times
+ Repeat one or more times
? Repeat 0 or one time
N Repeat n times
{N,} Repeat N or more times
{N,m} Repeat N to M times

Here are some examples of using duplicates:

Windows\d+ matches 1 or more digits behind windows

^\w+ matches the first word of a line (or the first word of the entire string, exactly what it means to look at the option setting)

Character class

To find numbers, letters, or numbers, white space is simple because you already have metacharacters that correspond to these character sets, but what if you want to match a character set that doesn't have predefined metacharacters (such as a vowel a,e,i,o,u)?

Very simply, you just have to list them in square brackets, like [aeiou] to match any English vowel, [.?!] Matches a punctuation mark (. or? or!).

We can also easily specify a range of characters, like [0-9] representing exactly the same meaning as \d: a number, and the same [A-z0-9a-z_] is exactly the same as \w (if only in English).

The following is a more complex expression: \ (? 0\d{2}[)-]?\d{8}.

"(" and ")" is also a meta-character, which is mentioned later in the Grouping section, so you need to use escape here.

This expression can match phone numbers in several formats, such as (010) 88886666, or 022-22334455, or 02912345678. Let's do some analysis of it: first an escape character \ (it can occur 0 or 1 times (?), then a 0, followed by 2 numbers (\d{2}), then a) or-or one of the spaces, it appears 1 times or does not appear (?), and finally 8 digits (\d{8}).

Branching conditions

Unfortunately, the expression just now matches 010) 12345678 or (022-87654321) of the "incorrect" format. To solve this problem, we need to use branching conditions. The branching condition in regular expressions refers to a number of rules that should be matched if any of these rules are met, by separating the different rules with a | Don't you understand? Okay, look at the example:

0\d{2}-\d{8}|0\d{3}-\d{7} This expression can match two phone numbers separated by a hyphen: a three-bit area code, a 8-bit local number (such as 010-12345678), a 4-bit area code, and a 7-bit local number (0376-2233445).

\ (? 0\d{2}\)? [-]?\d{8}|0\d{2}[-]?\d{8} This expression matches the phone number of the 3-bit area code, where the area code can be enclosed in parentheses or not, the area code and the local number can be separated by a hyphen or space, or there can be no interval. You can try branching conditions to extend this expression to also support 4-bit area codes.

\d{5}-\d{4}|\d{5} This expression is used to match the U.S. ZIP code. The rules of the U.S. ZIP Code are 5 digits, or 9 digits spaced with hyphens. The reason to give this example is because it illustrates a problem: when using branching conditions, be aware of the order of each condition . If you change it to \d{5}|\d{5}-\d{4} then it will only match the 5-bit ZIP code (and the top 5 digits of the 9-bit zip code). The reason is that when matching the branching conditions, each condition will be tested from left to right, and if a branch is satisfied, it will not be able to control the other conditions.

Group

We've already mentioned how to repeat a single character (just after the character is preceded by a qualifier), but what if you want to repeat multiple characters? You can specify sub-expressions (also called groupings) with parentheses, and then you can specify the number of repetitions of the subexpression, and you can do some other things with the subexpression (described later).

(\d{1,3}\.) {3}\d{1,3} is a simple IP-address matching expression. To understand this expression, parse it in the following order: \d{1,3} matches numbers from 1 to 3 digits, (\d{1,3}\.) {3} matches three digits plus an English period (this whole is the group) repeats 3 times, and finally adds a one to three digits (\d{1,3}).

Each number in the IP address cannot be greater than 255. Often people ask me, 01.02.03.04 such a number in front with 0, is not the correct IP address? The answer is: Yes, the number in the IP address can contain a leading 0 (leading zeroes).

Unfortunately, it will also match 256.300.888.999, an IP address that cannot exist. If you can use arithmetic comparisons, you may be able to solve this problem simply, but the regular expression does not provide any function about mathematics, so you can only use a lengthy grouping, select, character class to describe the correct IP address: ((2[0-4]\d|25[0-5]|[ 01]?\d\d?) \.) {3} (2[0-4]\d|25[0-5]| [01]?\d\d?].

The key to understanding this expression is to understand 2[0-4]\d|25[0-5]| [01]?\d\d, here I will not elaborate, you should be able to analyze the meaning of it.

Anti-righteousness

Sometimes you need to find characters that are not part of a character class that can be easily defined. For example, if you want to find any character other than the number, then you need to use the opposite justification:

Table 3. Commonly used antisense code
Code/Syntax Description
\w Match any characters that are not letters, numbers, underscores, kanji
\s Match any character that is not a whitespace character
\d Match any non-numeric character
\b Match a position that is not the beginning or end of a word
[^x] Matches any character except X
[^aeiou] Matches any character except for the letters AEIOU

Example: \s+ matches a string that does not contain whitespace characters.

]+> matches a string that starts with a in angle brackets.

Back to reference

When you specify a subexpression with parentheses, the text that matches the subexpression (that is, what this grouping captures) can be further processed in an expression or other program. By default, each grouping automatically has a group number, with the rule: left-to-right, with the left parenthesis of the group as the flag, the group number for the first occurrence is 1, the second is 2, and so on.

Uh...... In fact, the group number allocation is not as simple as I have just said:

    • Group 0 corresponds to the entire regular expression
    • In fact, the group number allocation process is to scan from left to right two times: first pass only to the unnamed group assignment, the second time only to assign the named group--so all named groups are larger than the unnamed group number
    • You can use the (?: EXP) syntax to deprive a group of the right to participate in group number assignment.

A back reference is used to repeat the search for text that precedes a grouping match. For example, \1 represents the text for grouping 1 matches. Hard to understand? Take a look at the example:

\b (\w+) \b\s+\1\b can be used to match duplicate words, like go go, or Kitty kitty. The expression is first a word, or more than one letter or number (\b (\w+) \b) between the beginning and end of the word, and the word is captured in a group numbered 1, followed by 1 or more whitespace characters (\s+). Finally, the content captured in Group 1 (that is, the word that preceded it) (\1).

You can also specify the group name of the sub-expression yourself. To specify a group name for a subexpression, use this syntax: (? \w+) (or replace the angle brackets with ' also: (? ') Word ' \w+) so that the \w+ group name is specified as Word. To reverse-reference the captured content of this group, you can use \k, so the previous example can be written like this: \b (? \w+) \b\s+\k\b.

There are many syntax for specific uses when using parentheses. Some of the most common ones are listed below:

Table 4. Common grouping syntax
category Code/Syntax Description
Capture (exp) Match exp, and capture text into an automatically named group
(? exp) Match exp, and capture the text to a group named name, or you can write (? ') Name ' exp ')
(?: EXP) Matches exp, does not capture matching text, and does not assign group numbers to this group
0 Wide Assertion (? =exp) Match the position of the exp front
(? <=exp) Match the position after exp
(?! Exp Match the position followed by the exp.
(? Match a location that is not previously exp
Comments (? #comment) This type of grouping does not have any effect on the processing of regular expressions, and is used to provide comments for people to read

We have discussed the first two syntaxes. The third (?: EXP) does not change the way regular expressions are handled, except that such groups of matching content are not captured in a group as in the first two, and do not have a group number. "Why would I want to do that?" "--good question, why do you think?"

0 Wide Assertion

Earth people, do not think these terms are too complex, too difficult to remember? I have the same feeling. Know that there is such a thing on the line, what it is called, go with it! If a person is nameless, he can concentrate on the sword, and if the object is nameless, he can choose freely.

The next four are used to find things before or after some content (but not including them), meaning they are used to specify a location like \b,^,$, which should satisfy certain conditions (i.e. assertions), so they are also called 0 wide assertions. It's best to take an example to illustrate it:

Assertions are used to declare a fact that should be true. In a regular expression, the match is resumed only if the assertion is true.

(? =exp) is also called a 0-width positive lookahead assertion, which asserts that the position behind itself appears to match the expression exp. For example, \b\w+ (? =ing\b) matches the front part of a word that ends with ing (except for parts other than ing), such as finding I ' m singing while you ' re dancing. It will match sing and Danc.

(? <=exp) also called 0 width is recalling the post assertion, which asserts that the front of the position itself appears to match the expression exp. For example (<=\bre) \w+\b matches the second half of a word that begins with re (except for parts other than re), such as when looking for reading a book, which matches ading.

If you want to add a comma to each of the three digits in a long number (of course, from the right), you can look for the part that needs to be preceded and added with a comma: ((? <=\d) \d{3}) +\b, which is 234567890 when it finds 1234567890.

The following example uses both of these assertions: (? <=\s) \d+ (? =\s) to match numbers separated by whitespace (again, these whitespace characters are not included).

Regular Expressions (full version)

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.