Regular Expressions of Linux

Source: Internet
Author: User
Tags alphabetic character posix egrep

The difference between a regular expression and a wildcard:

    • The command that most frequently applies regular expressions is grep (egrep), Sed,awk.
    • Regular expressions and wildcards are essentially different, and regular expressions are used to find: "File" content, text, string. Generally only three Musketeers support. Wildcard characters are used to find: file name. Common commands are supported

classification of regular Expressions

The POSIX specification divides regular expressions into two

    • Basic Regular Expression (BRE,basic regularexpression)
    • Advanced Features: Extended regular expression (ERE,extended regularexpression)

The difference between Bre and ere is only the difference between metacharacters

    • The BRE (underlying regular expression) admits only metacharacters with ^$. []* other characters recognized as ordinary characters: \ (\)
    • ERE (extended regular expression) adds () {}?+|, etc.
    • The character () {} is only treated as a meta-character in the Bre if it is escaped with a backslash "\", whereas in the BRE, any meta-symbol preceded by a backslash will be treated as a normal character.

Basic Regular Expressions
character Description
^ ^word: Search for content that starts with Word
$ word$: Search for content ending in Word
^$ Represents a blank line , not a space
. Represents and can only represent any one character (does not match a blank line)
\ Escape character, let the character with special meaning take off vest, show the prototype, like \. Denotes only the decimal point
* Repeats the previous character or text 0 or more, preceding the text or character 0 or more times in a row
.* Any number of characters
^.* Start with any number of strings,. * As much as possible, how much to count, greedy sex
[^ABC] Matches any character A or B or C that does not contain a ^, is an inverse of [ABC] and differs from the meaning of ^
A\{n,m\} Repeats the front a character N to M times (if you use Egrep or sed-r to remove the slash)

a\{n,\} repeat the previous a character at least n times, if you use Egrep or sed-r to remove the slash
A\{n\} Repeat the previous a character n times, if you use Egrep or sed-r to remove the slash
--- ---
extending the regular expression ere
Special Characters Meanings and examples
+ repeats the previous character one or more times, one or more of the previous characters, and takes the consecutive text/character out
? repeat the previous character 0 or 1 times (. Yes and only 1)
Pipe symbol | Indicates or filters multiple characters at the same time
() Grouping filters the things that are enclosed represent a whole (one character)

Pipe symbol |

(): Grouping filter is surrounded by something that represents a whole (one character)

The oldest text-matching program that uses the POSIX-defined basic regular expression (BRE) to match text.

    • Grep-e force grep to know the regular symbol directly, no need to escape
    • Egrep equivalent Grep-e is born to recognize regular symbols
    • We usually backup can be done through the form of CP file name {,. bak}, to avoid hitting the file name again
    • Sed-r: let sed support the regular

basic regular and extended regular differences
the base regular Bre Extended Regular Ere
\? ?
\+ +
\{\} {}
\( \ ) ()
\

The so-called basic regular is actually the need to escape the character mate expression of the regular, and the extension is to let the command extend its permissions so that he directly know the regular expression symbol (Egrep,sed-r,awk direct support)

Additional Information

1 Some pre-defined:

Regular Expressions Description Example
[: Alnum:] [a-za-z0-9] matches any one letter or number character [[: alnum:]]+
[: Alpha:] Match any alphabetic character (including uppercase and lowercase letters) [[: Alpha:]] {4}
[: Blank:] Spaces and tabs (Landscape portrait) [[: blank:]]*
[:d Igit:] Match any numeric character [[:d Igit:]]?
[: Lower:] Match lowercase letters [[: Lower:]] {5,}
[: Upper:] Match uppercase letters ([[: upper:]]+)?
[:p UNCT:] Match punctuation [[:p UNCT:]]
[: Space:] Match all whitespace characters including line feed, enter, etc. [[: space:]]+
[: Graph:] Matches any character that can be seen and can be printed [[: Graph:]]
[: Xdigit:] Any one hexadecimal number [[: xdigit:]]+
[: Cntrl:] Any one control character (the first 32 characters in the ASCII character set) [[: Cntrl:]]
[:p rint:] Any one of the characters that can be printed [[:p rint:]]

2 meta characters

Metacharacters is a Perl-style regular expression, and only a subset of the text processing tools support it, not all text processing tools support

Regular Expressions Description Example
\b Word boundaries \bcool\b matching cool, mismatched coolant
\b Non-word boundary cool\b matching coolant not matching cool
\d Single numeric character B\DB match Business-to-business, mismatch BCB
\d Single non-numeric characters B\DB matching BCB mismatch business-to-business
\w Single word characters (letters, numbers, and _) \w match 1 or a, mismatch &
\w Single non-word character \w match &, mismatch 1 or a
\ n Line break \ n matches a new row
\s Single whitespace character X\SX matches xx, does not match xx
\s Single non-whitespace character X\S\X matches Xkx, does not match xx
\ r Enter \ r Match Carriage return
\ t Horizontal tab \ t matches a horizontal tab
\v Vertical tab \v matches a vertical tab
\f Page break \f Match a page break

Reference Blog: http://www.cnblogs.com/chensiqiqi/p/6285060.html

Regular Expressions of Linux

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.