grep vs. regular Expressions (update ...)

Source: Internet
Author: User
Tags expression engine

grep function: Use the pattern to match the text in the specified file, is a very useful text search and filtering tools

Patterns: Regular expression characters and filter conditions for writing text characters

Regular expression: A pattern written by a class of special characters and text characters, some of which do not represent literal meanings, but represent control or wildcard functions, divided into basic regular expressions and extended regular expressions.

GREP uses the basic regular expression by default, Egrep or GREP-E to open an extended regular expression

--color=auto: Coloring A match to a character

-V: uncheck to show rows that cannot be matched by the pattern

-F: Using Fgrep

-P: Regular expression engine using the Perl language (unusually powerful)

-O: Show only the characters that match to (the entire line is displayed by default)

-I: Ignore character capitalization when matching

-Q: Silent mode, even if matched to or not output to the screen

-A N (n is a number): Displays the rows and rows followed by n rows

-B N: Displays the rows and rows preceding n rows

-C N: Displays the rows and rows before and after the row n

Basic Regular Expressions:

Character matching

. : means matching any single character

[]: matches any single character within the specified range

^: Characters outside of match range

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/A4/wKioL1XV7BiDTfUsAABiUskt0fg315.jpg "title=" 38.png "alt=" Wkiol1xv7bidtfusaabiuskt0fg315.jpg "/>

Number of occurrences: used after the number of characters to be specified, to specify the number of occurrences of the preceding character

*: Matches any of its preceding characters (0 to n times)

\?: matches the preceding character 0 or 1 times (the front \ is used to escape)

. *: Matches any character of any length

\+: Matches its preceding character at least once

\{m\}: Match its preceding character to the exact m-time

\{m,n\}: Matches the preceding characters at least m times up to N times

Example: Create a new text file Grep.txt with the following:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/71/A4/wKioL1XV7iuSLFxZAAAbnZJuTzI112.jpg "title=" 39.png "alt=" Wkiol1xv7iuslfxzaaabnzjutzi112.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/71/A8/wKiom1XV7GmRnSmdAAAymfeRWF0419.jpg "title=" 40.png "alt=" Wkiom1xv7gmrnsmdaaaymferwf0419.jpg "/>

Tip: The previous example matches to ' Xyxyxyxy ', so the regular expression works in greedy mode by default

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/A4/wKioL1XV71bBsW0TAAAiIDeyNx4853.jpg "title=" 41.png "alt=" Wkiol1xv71bbsw0taaaiideynx4853.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/71/A4/wKioL1XV77jA3PsUAAA0dJkIyU8995.jpg "title=" 42.png "alt=" Wkiol1xv77ja3psuaaa0djkiyu8995.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/A8/wKiom1XV7wWiEkDcAAA47VCqCWE955.jpg "title=" 43.png "alt=" Wkiom1xv7wwiekdcaaa47vcqcwe955.jpg "/>

Location anchoring:

^: Anchoring the beginning of the line

$: Anchor Line End

^$: Anchor Empty Line

^[[:space:]]*$: Anchoring blank lines (including tabulation, spaces)

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/A5/wKioL1XV8Zjw-QBWAAD3Wkjlpp0226.jpg "title=" 44.png "alt=" Wkiol1xv8zjw-qbwaad3wkjlpp0226.jpg "/>

Word anchoring

\< or \b: The first anchor of the word should appear on the left side of the word pattern

\> or \b: The ending anchor should appear on the right side of the word pattern

\b<pattern>\b: Anchoring the whole word

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/71/A5/wKioL1XV8z2zKu_AAAB_qTT3VcM180.jpg "title=" 45.png "alt=" Wkiol1xv8z2zku_aaab_qtt3vcm180.jpg "/>

Grouping: Bundling one or more characters together as a whole

\(\)

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/71/A5/wKioL1XV88rBZSTBAAA2iqqJFTg480.jpg "title=" 46.png "alt=" Wkiol1xv88rbzstbaaa2iqqjftg480.jpg "/>

Back reference: Refers to the character that the pattern in the preceding grouping brackets matches to

\1: matches the matching character between the first opening parenthesis and the closing parenthesis that match from the left

\2: matches the matching character between the second opening parenthesis and the closing parenthesis that match from the left

\ n: matches the matching character between the nth opening parenthesis and the closing parenthesis that matches from the left

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/71/A5/wKioL1XV9RCB8qv3AABqIjpMHiE271.jpg "title=" 47.png "alt=" Wkiol1xv9rcb8qv3aabqijpmhie271.jpg "/>

This article is from the "After Tomorrow" blog, please be sure to keep this source http://leeyan.blog.51cto.com/8379003/1686693

grep vs. regular Expressions (update ...)

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.