1. What is a regular expression and what is its function.
Global Search Regular Expression and print out the line.
Regular Expression: Regular Expression Engine
Regular Expression:
Is a type of character writing mode, many of which do not represent its literal meaning, but express control or wildcard functions,
The text search tool performs a row-by-row search on the target file based on the text mode (Search Condition) specified by the user to display matching rows.
Basic Regular Expression: grep
Extended Regular Expressions: egrep, grep-e
Fgrep: fast. Regular Expressions are not supported.
Syntax format:
Grep [Option]... 'pattern' file...
-- Color = auto
Command Options:
-V: Reverse Selection
-O: only display the matched string, not the row of the string.
-I: ignore-Case, Case Insensitive
-E: supports the use of extended regular expressions.
-A # After Matching content # Row Information
-B # Before matching content # Row information (the first matching item is the first line)
-C # Matching content before and after # Row Information
2. Differences between basic regular expressions and extended character expressions
Metacharacter of the basic regular expression: character matching
Times matching metacharacters: used to specify the number of times a character before it can appear
A string of consecutive characters that do not contain special characters is called a word.
Regular number matching metacharacters |
Han Yi |
Extended regular number matching metacharacters |
Han Yi |
* |
Any length. The first character can appear at any time. |
* |
Any length. The first character can appear at any time. |
\? |
0 or 1 time |
? |
0 or 1 time |
\ {M \} |
MB |
{M} |
MB |
\ {M, N \} |
At least m times, up to n times |
{M, n} |
At least m times, up to n times |
\ {M ,\} |
At least m times |
{M, 0} |
At least m times |
\ {0, n \} |
Up to n times |
{0, n} |
Up to n times |
.* |
Any character of any length |
+ |
At least 1 time |
3. Character matching
Character matching |
Han Yi |
. |
Match any single character |
[] |
Match any single character in the specified range |
[[: Alpha:] |
Uppercase/lowercase letters |
[[: Alnum:] |
Uppercase/lowercase letters |
[[: Digit:] |
Number |
[^] |
Invert |
4. Positioning
Character |
Han Yi |
^ |
The first line is anchored at the right of the write mode |
$ |
At the end of the line, write the string at the right of the pattern. |
^ $ |
Blank line |
\ < |
The beginning of the word, on the left side of the word, \ B \ <char |
\> |
At the end of the word, on the left side of the word, \ B \ <char |
5. Grouping
The pattern matching content in the group can be stored in the memory by the Regular Expression Engine and can be referenced later.
\(\)
Example: \ (AB \)*
Reference:
For example, \ (AB \ (x \) Y \). * \ (Mn \)
Numbered: Left brackets (from left to bottom) and matching right brackets
\ (A \ (B \ (C \) Mn \ (x \). * \ 1
For example:
\ (AB \? C \). * \ 1
Abcmnaaa
Abcmnabc
Abcmnac
Acxyac
Fgrep 'pattern' file...
Regular expressions are not supported.