Linux Regular expressions

Source: Internet
Author: User
Tags alphabetic character file url perl regular expression egrep

First, the Linux text Lookup command

Before talking about Linux regular expressions, there is one thing that needs to be explained in advance that there are three commands commonly used to find text files under Linux:

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

2.egrep: Extend grep, which uses an extended regular expression (ERE) to match the file.

3.fgrep: Fast grep, this version matches a fixed string rather than a regular expression, and is the only version that can match multiple strings in parallel.

It should be stated that Egrep and Fgreo can no longer be used directly in the latest version, and are replaced with GREP-E and grep-f.

grep--help can be used to query grep for more information:

Regular Expression Selection and interpretation:-E,--extended-RegExp PATTERN is an extensible regular expression (abbreviated as ERE)-F,--fixed-Strings PATTERN is a set of fixed-length strings separated by a break character. -G,--basic-regexp PATTERN is a basic regular expression (abbreviated as BRE)-P,--perl-regexp PATTERN is a Perl regular expression-E,--regexp=pattern is used for matching operations with pattern.-F,--file=file gets the PATTERN from file-I.,--ignore- CaseIgnore Case-W,--word-RegExp enforce PATTERN only matches words exactly-X,--line-RegExp forcing PATTERN to match only one line exactly-Z,--NULL-data A0byte of the data row, but not a blank line miscellaneous (other options):-S,--no-messages suppress error messages to eliminate false messages-V,--invert-matchSelectnon-matching lines Select a non-matching line, that is, take the inverse-V,--versions display version information and exit information--help Display ThisHelp text and exit output control:-M,--max-count=num num times after match stop-B,--byte-offset output at the same time print byte shift-N,--line-number output with line numbers printed at the same time--line-buffered each line of output is emptied-H,--with-FileName Prints the file name for each match-H,--no-filename prefix is not displayed on filename output--label=label prefixes the label as a standard input file name-O,--only-matching show only part of a line matching pattern shows only partial results-Q,--quiet,--silent Suppress all normal output disables all regular outputs--binary-files=type assume that binary files is type; Suppose you need to match a binary file type is 'binary','text', or'Without-match'-A,--text equivalent to--binary-files=Text equivalent to binary file = text-I equivalent to--binary-files=without-match is equivalent to binary file = No match is made to the-D,--directories=ACTION how to handle directories; How to work with directories: Read, recursive, skip ACTION is 'Read','recurse', or'Skip'-D,--devices=action How to handle devices, FIFOs and sockets; What to do with devices, first-in, first-out queues, and socket ACTION is 'Read'Or'Skip' Read or skip-R,--recursive like--directories=recurse Recursion-R,--dereference-recursive references recursive likewise, but follow all symlinks--include=File_pattern contains file mode search only files this match File_pattern only find file--exclude=File_pattern does not contain file mode skip files and directories matching File_pattern--exclude- from=file Skip files matching any FILE pattern fromFILE--exclude-dir=The pattern directories that match PATTERN would be skipped. The matching directory is skipped-L,--files-without-match print only names of FILEs containing no match prints mismatched filenames only-L,--files-with-matches print only names of FILEs containing matches matching file name-C,--count Print only a count of matching lines per file print matches the number of lines to each of the files-T,--initial-tab make tabs line up (ifneeded) tab alignment if required-Z,--NULLPrint0 bytefile File control is not printed after the after file name filename:-B,--before-context=num Prints num lines starting with text-A,--after-context=num Prints num lines that end with text-C,--context=num Print output text num line-num Same as--context=NUM--group-separator=sep Use SEP asa group separator uses Sep as a separator--no-group-separator Use emptystring  asa group separator--color[=When ],--colour[=When ] use markers to highlight the matching strings; when is ' always','never', or'Auto'-U,--binary DoNot strip CR characters at EOL (msdos/Windows)-U,--unix-byte-offsets report Offsets as ifCRs were not there (MSDOS/Windows) ' egrep ' is ' grep-E '. ' Fgrep ' is ' grep-F '. Direct use of ' egrep ' or ' fgrep ' is not available. If file is-The standard input will be read. Reads the current directory without file, unless specified on the command line-r option. If there are fewer than two file parameters, it will be used by default-h parameter. If any rows are matched, the exit status is0, otherwise the1If an error occurs, and no-Q parameter, the exit status is2。

Second, the regular expression

1. Composition of regular expressions

(1) General characters: characters with no special meaning

(2) Special characters (meta characters): metacharacters, which have special meanings in regular expressions

2. Meta-character Introduction

(1) Bre and ere (GREP-E) generic meta characters

Meta character Function
\ Typically used to turn on or off special meanings of subsequent characters, such as \ (... \)
. Match any single character (except null)
* Match any of the preceding or none of the individual characters, for example. * matches any length of any character
^ Match what begins with a character
$ Match the character ending with
[] Matches any character within the parentheses, using the connector (-) to represent the range, (^) for the inverse

(2) meta-characters to be used by BRE (grep)

Meta character Function
\{n,m\} Interval expression that matches the number of occurrences of a single character in front of it, \{n\} reproduces n times, {\n,m}\ reproduces N to M times
\(\) Reserved space, you can store up to 9 independent sub-patterns in a single pattern, with parentheses as a whole
\ n Repeat in \ (and \) square brackets The pattern of the nth child mode to this point

(3) ERE (GREP-E) to use meta-characters

Meta character Function
{N,m} The number of times limit, the same as the Bre \{n,m\} function, not to find out a field
+ Match one or more of the preceding
Match one or more of the preceding
| Match | A regular expression before or after a symbol, logical, or
() Matches a regular expression group enclosed in square brackets.

Comparison:

grep -E "e{1,3}"2grep"e\{1,3\} " 2 . txt qweoooewwooaaaaoaaaafaweeeeeeababyyyyyyyyyeeeabababuuu

(4) Character set

The identity character set has the following methods:

[:: Alnum]: Numeric characters [:d igit:]: Numeric characters
[:p UNCT:]: Punctuation characters [: Alpha:]: Alphabetic character
[: Graph:]: non-whitespace character [: Space:]: space character
[: Blank:]: spaces and positional characters [: Lower:]: Lowercase alphabetic characters
[: Upper:]: Uppercase characters [: Cntrl:]: Control character
[:p rint:]: Characters that can be displayed [: Xdigit:]: 16 binary digits

Third, examples

The following is a common example to learn the Bre and ere matches, the source file Url.txt content as follows:

www.baidu.comhttp: // www.baidu.comhttps://www.baidu.comhttp://wwwbaiducom  Baidu.combaidu

1.url Matching

Match begins with HTTP or HTTPS and is followed by: and contains a string of.

Bre match:

grep ' ^https\{0,1\}.*\. *' url.txt

Ere match:

grep ' ^https?. *\.. *' url.txt

The matching results are as follows:

http://www.baidu.comhttps://www.baidu.com

2.Email Matching

The sample file contents are:

[Email protected] [Email protected] [Email protected] [email protected]@ @baidu. com

Matches multiple characters beginning with an alphanumeric or underscore, followed by an @ followed by a number of alphanumeric or underscores, with one.

grep ' ^[[:alpha:][:d igit:]_]*@[[:alpha:][:d igit:]]*\. *' email.txt

Matching results:

[Email protected] [Email protected] [Email protected]

Linux Regular expressions

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.