Regular Expressions (i)

Source: Internet
Author: User
Tags uppercase letter

First, Regular Expressions:

A regular expression (or regular expression, or re) is a text pattern consisting of ordinary characters (such as characters A through Z) and special characters (called metacharacters).

This pattern describes one or more strings to match when looking up a text body.

A regular expression, as a template, matches a character pattern to the string you are searching for. Simply put, the regular expression is the method of processing the string, it is the behavior unit to the string processing behavior, regular expression through some special symbols of the auxiliary, can let the user easily reach the search/delete/replace a particular string handler. Regular expressions are supported by commands such as Vim, grep, find, awk, sed, and so on.

Common Regular Expressions:

1,. Represents any single character, such as:/L. e/with an L, followed by two characters, and then matched with a line of E

2, ^ represents the beginning of the line. ^love: Matches all lines beginning with Love

3, the $ represents the end of the line. love$ such as: Match all love end lines

so ' ^$ ' it means blank lines.

4. [...] Match one of the characters in parentheses

[ABC] matches a single character A or B or C

[123] matches a single character 1 or 2 or 3

[A-z] matches one of the lowercase letters A-Z

[A-za-z] matches any of the English letters

[0-9a-za-z] matches any English letter or number one

Note: The single and one of the red ones above, no matter how complex it is, the result is a character!

You can use the ^ tag to do a prefix within [] to denote characters other than the characters in the []. For example, the search for a string without G before OO line. Apply ' [^g]oo ' as the search string, the^ symbol if it appears in the starting position of [] negative, but the other position in [] is the ordinary character. [^ab^c] matches any single character of B or ^ or C or not a

5. * Used to decorate leading characters, indicating leading characters appear 0 or more times

such as: ' A*grep ' matches all 0 or more aces followed by the line of grep. ". *" means any string

6, \? Used to decorate a leading character, indicating that a leading character appears 0 or 1 times

A\? Match 0 or 1 a

7. \+ is used to modify the leading character, indicating that the leading character appears 1 or more times

a\+ match 1 or more a

8, \{n,m\} is used to decorate leading characters, indicating that leading characters appear n to M times (N and M are integers, and n<m)

A\{3,5\} matches 3 to 5 consecutive a

There are several other forms of \{n,m\}:

\{n\} consecutive n leading characters

\{n,\} at least n leading characters in a row

9, \ is used to escape a single special character immediately following it, so that the special word literal the ordinary character

such as: ^\. [0-9] [0-9] pairs with a period and two numbers start

For example:

A * matches any successive (also includes 0) a

A\? Match 0 or 1 a

a\+ match 1 or more a

A\{3,5\} matches 3 to 5 consecutive a

\.* Match 0 or more consecutive. \. Indicates a normal character period

Ten , | represents or such as: a|b|c matches a or b or c . such as:grep|sed matching grep or sed

One , () to synthesize part of the content into a unit group, such as to search for glad or good can be as follows ' G (la|oo) d '

General Example 1:

1 Christian Scott lives here and would put on a Christmas party.

2 There is around to people invited.

3 They is:

4 Tom

5 Dan

6 Rhonda Savage

7 Nicky and Kimerly.

8 Steve, Suzanne, Ginger and Larry.

^[a-z].. $

The search line begins with a letter A to Z, followed by two arbitrary letters, followed by a line with a newline character. The 5th line will be found.

^[A-Z][A-Z]*3[0-5]

The search begins with an uppercase letter followed by 0 or more lowercase letters, then a number 3, followed by a number between 0-5. Could not find match line (change to ^[a-z][a-z]*.*3[0-5] to find line 2nd)

^ *[a-z][a-z][a-z]$

Search begins with 0 or more spaces, followed by an uppercase letter, two lowercase letters, and a transfer character. The 4th row of Tom (the whole row match) and the 5th row will be found. Note that * There is a space in front.

^[a-za-z]*[^,][a-za-z]*$

The lookup starts with 0 or more uppercase or lowercase letters, does not follow a comma, and then is followed by 0 or more uppercase or lowercase letters, followed by a transfer character. Rows 4th and 5 will be found.

General Example 2:

# ls-l/bin | grep ' ^...s '

The above command is used to find the suid file;

# LS-LR/USR | grep ' ^...s. S

The above command is used to find the suid and GUIDs.

Second, grep Use of Commands

grep (Global search Regular expression (RE) and print out of the line, full search of regular expressions and print out rows) is a powerful text search tool that uses regular expressions to search for text. and print out the matching lines.

Parameters:

1.-A Num,--after-context=num is listed in addition to the line, and the following NUM lines are listed.

For example: $ grep–a 1 Panda file (search for rows with panda style from file, and display the following 1 rows of the row)

2.-B Num,--before-context=num is relative to-a num, but this parameter is a num line that displays in addition to the line that matches and appears before it. such as: (Search for panda-style rows from file and display the first 1 rows of the row)

$ grep-b 1 Panda file

3,-C [NUM],-num,--context[=num] list rows outside the line and list the upper and lower NUM rows, the default value is 2.

such as: (list file In addition to the line containing the panda style and out of its top and bottom 2 lines) (to change the default value, directly change num)

$ grep-c[num] Panda file

4,-C,--count does not display matching style lines, only shows the total number of rows that meet. If you add-V,--invert-match, the parameter shows the total number of rows that are not met

5,-i,--ignore-case ignoring the case difference

6.-n,--line-number Print line number in front of matching line

7,-v,--revert-match Anti-retrieval, only show unmatched rows

8. Exact match:

For example, when extracting the string "48", The returned result contains other strings that contain "48" such as 484 and 483, and the rows containing only 48 should actually be extracted exactly.

An effective way to extract exact matches using grep is to add \> after extracting the string. Assuming that the exact 48 is now extracted,

Here's how:

#grep ' 48\> ' filename

9.-S does not display error messages that do not exist or have no matching text

such as: Execute command grep "root"/etc/password, because the password file does not exist, so output error message on the screen, if using the grep command-s switch, can block the error message

To use a good grep this tool, in fact, is to write a regular expression, so here does not have all the functions of grep to explain, only a few examples, explain a regular expression of the wording.

$ ls-l | grep ' ^d '
Filters the contents of the Ls-l output through a pipeline, displaying only the lines that begin with D.

$ grep ' test ' d*
Displays all rows that contain test in a file that begins with D.

$ grep ' test ' AA bb cc
Displays the line that matches test in the aa,bb,cc file.

$ grep ' [a-z]\{5,\} ' AA
Displays all rows that contain a string of at least 5 consecutive lowercase characters for each string.

$grep ' t[a|e]st ' filename

Displays all rows that contain test or tast.

$grep ' \.$ ' filename

Displays all rows that end with.

Regular Expressions (i)

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.