A summary of the regular

Source: Internet
Author: User

Regular Expressions (Regular expression) are patterns used to describe a set of string characteristics that match a particular string. Use special characters + ordinary characters to describe the pattern to achieve the text matching purpose tool. Similar to the common in the life of the revelation, by describing a person's characteristics to conduct "search matching"
Now it is widely used by us and is now integrated into a variety of text editor/Text Processing tools

Application Scenarios
    • Verify: User name password verification is performed when form is submitted.
    • find: quickly extract the specified content from a large amount of information. In a batch of URLs, find the specified URL
    • Replace: The specified format of the text, the regular matching lookup, found after the specific replacement, (vim text replacement, etc.)
      In many technical fields (for example, natural language processing, data storage, etc.), regular expressions can easily extract the information we want, so this part is essential
form the basic elements
    • Character class
    • Quantity qualifier
    • Position Qualifier
    • Special symbols
1. Character classes:
character Description Example
. Match any one of the characters ABC. can match ABCD, ABC0, etc.
[] Match any one of the characters in [] [012]a can match 0a, 1a, 2a
- To represent a range of characters within parentheses such as [0-9] can match any one number
^ Place in front of [] to match any character except for characters in parentheses [^ab]c can match 1c, DC, but cannot match AC, BC
[[: XXX:]] Some named character classes that are predefined by the grep tool [[:d Igit:]] can match a number, [[: Alpha:]] matches a character, [[: Lower:]] matches any lowercase letter, etc.

Application:

  1. grep uses the--color option to label the matched string in red
  2. Under Linux, you can use echo $ to print the exit code executed by the previous command, which is 0 for successful execution and 1 for failure.

The experiment is as follows:

Note: use . The default is greedy matching, which is related to the following regular matching method, which is described later.

2. Quantity qualifier:
character Description Example
? Match the cell immediately preceding it (a number or character in front of it) 0 or 1 times If you match decimals, use 0\.? [0-9] Match 0.1, 0.2, 0.3, etc.; In the regular is a special symbol so you need to use \ to escape the operation (again later)
+ Match the unit immediately preceding it 1 or more [A-za-z0-9_.-][email protected][a-za-z0-9_.-]+\.com matches an email address
* Match the unit immediately preceding it 0 or more [0-9][0-9]* matches at least one digit, equivalent to [0-9]+
N Exact Match of the cell immediately preceding it n times [0-9] {3} matches numbers from 000 to 999
{N,} Match the cell immediately preceding it at least n times [0-9] {3,} matches three digits and above
{, M} Matches the unit immediately preceding it up to M times [0-9] {, 1} is equivalent to [0-9]?
{N,m} Match the cell immediately preceding it n~m times Approximate match IP address:[0-9]{1,3}\.[ 0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}

Application:

3. Position qualifier:
character Description Example
^ Matches the beginning of the line, starting from the beginning ^world matches only the world at the beginning of a row
$ Matches the end of the line, starting at the end of the line $ Match a line at the end;^$ matches a blank line
< Match word start position < th matches this, does not match teach, Ethernet
\> Match end of Word position p\> matches sleep, leap, and so on, does not match parent, sleepy
\b Match the start and end position of a word such as \borld match World, Aorld,\borld\b only match Orld
\b Matches the start and end position of a non-word Words such as \bat\b match battery, do not match attend, hat, etc. with the string "at" beginning and ending

Note: where \b is used to limit whether there are words in the target string that begin with the specified string, we call it the word boundary. \b called the non-word boundary
Application:

4. Special symbols:
character Description Example
\ Escape character, ordinary character escapes as special characters, special character escapes as ordinary characters < writes < matches the beginning of the word, preceded by \ Write \. The literal value of the fetch .
() To enclose a part of a regular expression as a unit, you can use the quantity qualifier for the entire cell. ([0-9]{1,3}\.) {3} [0-9] {3} Matching IP address
| Connecting two sub-expressions, representing or relationships N (o|either) matches no or neither

Application:

() encloses the content as a whole, and thus is qualified by the quantifier.

| Used to cascade multiple conditions, as long as there is any one match, can match, representation or relationship, we call the disjunction ,

Regular expression version other common universal character sets and their substitution
symbols equivalent to Match
\d [0-9] numeric characters
\d [^0-9] Non-numeric characters
\w [A-za-z0-9_] Digital Letter Underline
\w [^\w] Non-alphanumeric underline
\s [_\r\t\n\f] Blank areas such as tables, line breaks, etc.
\s [^\s] Non-blank area

So, we can now use these symbols to simplify the writing of our regular expressions? Give it a try

But the results don't seem as good as we wanted? In fact, this is also related to the regular expression version. The regular is divided into the following versions:

  • Basic Regular Expressions (basic Regular expression also known as basic RegEx abbreviation BREs)
  • Extended Regular expression (Extended Regular expression also known as Extended RegEx abbreviation EREs)
  • Perl Regular Expressions (Perl Regular expression called Perl RegEx abbreviation PREs)

Specify the appropriate parameters in grep, and the default in these versions is the basic regular, with the-e option to extend the regular, and with the-p parameter is the Perl version of the regular. To solve the previous problem, let grep take the-p option here to resolve the

Differences between versions
    • The extended specification of regular expressions is basically the same as the basic specification. Just under the basic specification, some characters ? +{}| () should be interpreted as ordinary characters, and to indicate the above special meanings need to be escaped.
    • Conversely, under the extended specification, the +{}| () should be understood as a special meaning, to take its literal value, but also to be escaped.
    • Therefore, the grep tool with the-e option means that the match is made using the extended regular, and if not, the match is made using the datum regular. Or simply use the egrep command to operate
    • The Perl regular match with the-P option is one of the most important features of the integration of the Perl language, it is very powerful, and the regular expressions of Perl are basically referred to in many language design support.
Regular match mode
  • Greedy mode
    When a regular expression goes to match, as much as possible matches the content of the condition grep defaults to a greedy match.
  • Non-greedy mode
    Regular expression to match, will be as little as possible to match the conditions of the content that is, once the match is found to meet the requirements, immediately match success, and will not continue to match (unless there is g, open the next set of matches)

Summary: you can see that the non-greedy pattern identifier is followed by the greedy pattern identifier plus one?

A summary of the regular

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.