1. What is a regular expression?
To put it simply, regular expressions are a set of rules and methods that are defined to handle a large number of strings.
For example: Suppose "@" stands for boy, "!" Represents girl. echo "@!" = = = "Boygirl"
By defining the assistance of these special symbols, the system administrator can quickly filter, replace, or output the required strings.
Linux regular expressions are typically handled in a behavioral unit.
2. Why should I learn regular expressions?
In the enterprise work, we do every day in the Linux operations, the moment will face a large number of strings with text configuration, programs, command output and log files, etc., and we often have an urgent need, from a large number of string content to find matching the work of the specific string. This depends on the regular expression. Therefore, it can be said that the regular expression is to filter the requirements of these strings are born.
3. Easy to confuse two notes:
A. Regular expressions are widely used in a variety of languages, such as Php,python,java. However, what we are talking about today is the regular expression of Linux system operations, that is, the Linux regular expression, the most commonly used regular expression command is grep (egrep), Sed,awk, in other words, the Linux Three Musketeers want to work more efficient, It must be inseparable from the regular expression.
B. Regular expressions and our usual wildcard characters are fundamentally different, which is important to note.
Wildcard example: LS *.log here is the wildcard character (for all), not a regular expression.
First, the basic regular wave character description:
1) ^word matches the content that begins with Word. Vi/vim Editor ^ represents the beginning of a line
2) word$ matches the content ending with Word. Vi/vim Editor $ Represents the end of a line
3) ^$ indicates blank line
Second, the basis of a regular two-wave character description:
4). Represents and can only represent any one character
5) \ Escape symbol, example \. Just represent the point itself, let the character with special meaning take off the vest and restore the prototype
6) * Repeat 0 or more of the preceding one character, example 0* matches no 0, there is a 0 or more 00000
7). * Matches all characters. Extend ^.* begins with any number of characters: *$ ends with any number of characters
Tip: A summary of the special meanings of points (.):
1. Current directory
2. Make the file effective equivalent to source
3. Hide Files
4. Any one character (grep regular)
Three, the basic regular third Wave character description:
8) [ABC] matches any one of the characters in the character set [a-za-z],[0-9]
9) [^ABC] matches the contents of any character that does not contain ^
^ in brackets for inverse, note and brackets outside with ... Beginning Difference
Ten) a\{n,m\} repeats N to M times, before a repeating character. If you use Egrep/sed-r, you can remove the slash
A\{n,\} repeats at least n times, preceding a repeating character. If you use Egrep/sed-r, you can remove the slash
A\{n\} repeats n times, before a repeating character. If you use Egrep/sed-r, you can remove the slash
A\{,m\}
Note: egrep (grep-e) or Sed-r filter General special characters can not be escaped (without \)
Summary of the Three Musketeers "grep"
grep General Common parameters:
-A: Search for data in a binary file as a text file
-C: Count the number of ' search strings ' found
-O: Only the content that matches RegExp is displayed (used to count the number of occurrences in the article)
-I: Ignores case differences, so case is considered the same
-N: Match content displays line number at the beginning of its start
-V: Reverse selection, which shows the line without the ' search string ' content
-E: Expanded grep, i.e. Egrep
--color=auto: Highlight matching keywords in a specific color
Tip:-i,-v is a common parameter
-a:after, displaying data that matches a string and its following n rows
-b:before, displaying data that matches a string and its first n rows
-c:context, displays the matching string and its front and back num lines
Basic part of Linux regular expression