Linux regular expression-metacharacters

Source: Internet
Author: User
Tags character classes egrep

Linux regular expression-metacharacters

We have read two basic elements in the expression:
1. A value expressed as a nominal value or variable.
2. An operator.
Regular Expressions are composed of these identical elements. Except for metacharacters, they are interpreted as matching only the literal value of the metacharacters.
Metacharacters

Special characters Purpose
. Match any single character except line breaks. In awk, periods can also match line breaks.
* Match any character (including zero characters) before it (including characters specified by the regular expression)
[...] Match any of the character classes in square brackets. If the first character in square brackets is a strip character (^), it indicates a negative match, that is, it matches all characters except linefeeds and those listed in the class. In awk, the line break is also matched. Hyphens (-) are used to indicate the character sphere. If the first character in the class is right square brackets (]), it indicates that it is a member of the class. All other metacharacters will lose their original meaning when they are specified as class members.
^ If it is the first character of a regular expression, it indicates the start of matching the row. Match the start of a string in the awk, even if the string contains an embedded line break.
$ If it is the last character of the regular expression, it indicates the end of the matched row. Match the end of a string in the awk, even if the string contains an embedded line break.
\ {N, m \} Match the number of times a single character appears in a certain range before it (including characters specified by a regular expression ). \ {N \} will match n times, \ {n, \} will match at least n times, And \ {n, m \} matches any occurrence between n and m. (Only the earlier versions of sed and grep are unavailable ).
\ Escape subsequent special characters.

 

Extended metacharacters (egrep and awk)

Special characters Purpose
+ Match the regular expression once or multiple times
? Matches zero or one occurrence of the previous regular expression.
| Specify the regular expression that can match the previous one or the subsequent one (alternative)
() Group Regular Expressions
{N, m} Match the number of times a single character appears in a certain fan circle (including characters specified by a regular expression ). {N} indicates matching n times, {n,} matches at least n times, and {n, m} matches any occurrence between n and m. (For POSIX egrep and POSIX awk instead of traditional egrep or awk)

 

Metacharacters have special meanings in regular expressions, which are the same as those in ++ and * arithmetic expressions. Some metacharacters (+? () |) Is available only when it is part of an extension set used by programs (such as egrep and awk. In the following sections, we will introduce the usage of each metacharacter, starting with a backslash.

 

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.