Regular expressions in commands
If you want to use fuzzy lookup when filtering content in command output or text, you need to use regular expressions. A regular expression is a set of fuzzy search patterns composed of multiple meta characters. Using regular expressions, you can quickly find and locate content specified in text.
1. Single-character matching character.
Regular expressions are mainly composed of some meta characters and matching patterns.
Can a single character match character match any single character? The function of this character is and the file name match character? Same function
Find text using regular expressions,First, you need to use meta characters to form a search pattern
(1) When using the search mode, it is usually put into two slashes // and then put into a command. For example, to find a matching pattern /.i..../ in a text, the possible options are: as long as the lowercase letter does not appear at the beginning of the line, or the end of the line has 5 characters to match.
2. Single character or string duplicate match characters*
Used to match a single character or a sequence of strings once or multiple times.
3. Line head matching character^
Used to indicate the string or pattern at the first position of the line in the match. When using it, you need to place the line head match character in front of the string or pattern to match.
For example, for the command ls -1, use the line header to match /^1/, and the results start with 1
4. End of line matching character $
Used to match strings or patterns at the end of each line of text, and when used, put the end of line match characters after the string or pattern.
For example, match all lines with love at the end of the line in text
/love$/
5. Backslash shielding symbol\
This character is used to block the special meaning of some special characters. Common special characters may be quote symbols, comment symbols, wildcard characters and logical operators, etc. The following lists some special symbols.
Comments, separating symbols: #,; etc.
Quotation symbols: $, ``, "" etc.
Metacharacters: ., *, ^, $,? ,[],\wait
Logical operators or operators: ||, &&, etc.
When using these characters in the command, you must use a backslash to block their special meaning.
For example, if you use multiplication symbols* for multiplication operations, you must use a backslash to block its special meaning for use.
# expr 256 \* 256
6. Range match character[] and exclude range match character[^]
The usage of range match characters in file name wildcards is basically the same.
(1) Match the words love and love:
/[L]ove/
(2) Does not match all letters:
[^a-z]
7. Matching characters \< and \>
Used to match specific characters or patterns at the beginning and end of a word
(1) For example, to match text with disc as the beginning of the word, you can use
/\<disc/
(2) To match text ending with a ment, you can use:
/ment\>/
8. The number of repetitions match character "x\{\}"
It can fully accurately match the number or range of times a character or string appears continuously.
(1) To match text with letter m that appears 5 times, you can use:
/m\{5\}/
(2) To match text with letter m that appears at least 5 times, you can use:
/m\{5,\}/
(3) To match text with letter m that appears 5 to 10 times, you can use:
/m\{5,10\}/
9. Combine and use regular expressions
(1) When searching and filtering information, it is sometimes necessary to remove blank lines in the text. Since blank lines do not have any characters, you can use the beginning and end of the line matching characters to match blank lines.
/^$/
(2) Sometimes, in order to look more beautiful, some text will use a character to fill the entire line, and you can use the following pattern to match these lines:
/^.*$/
(3) Match all lines starting with the or The
/^[tT]he/
(4) Exclude all lines starting with the and The
/^[^Tt]he/
(5) Match an IP address:
/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/
(6) Match a string composed of 6 letters. The first two characters are letters, the middle two characters are 22 and the last two characters are lowercase letters:
[a-z]\{2\}22[a-z]\{2\}
(7) To accurately match the word love:
/\<love\>/
The above is a detailed explanation of the regular expressions in Linux commands introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website