|
What are regular expressions? Regular expressions are syntax rules used to describe the character arrangement and the matching pattern. In many programming languages support the use of regular expressions for string manipulation, in different languages of regular expressions is slightly different, but after all, regular, its essential idea is the same, when we have the shell regular, go and python or perl inside positive expression, you will find it is really the same stuff.
In some command shell, some do not support regular expressions, but they support Linux inside the wildcard is the wildcard is what stuff it, it is with regular expressions what is the relationship?
Regular expressions for qualified string matching in the file, that contains a regular match. grep, awk, sed, etc. commands support regular expressions. Wildcard to match qualified filename, wildcards are an exact match. ls, find, cp These commands do not support regular expressions, we can only use their own shell wildcard to match.
Wildcards are mainly the following three:
* Matches any character
? Matches any one content
[] Matches a character in brackets
First, the regular expression is used in this document Chinese string matching, and wildcards are used to match qualified filename; secondly contains a matching regular expression is, as long as a regular string that contains just match, but the wildcard is exact, that is, we will have to match exactly with the criteria string.
Having said that, in fact, a regular expression is mainly used for string pattern segmentation, matching, find and replace operation, let's look at the basic regular expression metacharacters and its role.
Metacharacters effect illustration
* Match the previous character 0 times or as many times as "a *" matches everything, including blank lines
"Aa *" matches containing at least one of a row
"Aaa *" matches the last two consecutive contain a string
"Aaaaa *" contain a minimum of four consecutive matches a string
. Matches any character except a newline
"S..d" s and d match between these two letters must have two
Word character
"S. * D" match between s and d alphabetic characters arbitrary
. "*" Matches everything
^ Matches the beginning of the line "^ hello" hello to match the beginning of the line
"^ M" matching rows in uppercase "M" at the beginning of
$ Matches the end of the line "hello $" matches the end of the line hello
"N $" matches lowercase "n" at the end of the line
"^ $" Matches blank lines
[] Matches any one character in brackets specified, only matching a
Characters
"[Aeiou]" matches any vowel,
"[0-9]" matches any digit,
"[A-z] [0-9]" matches lowercase letters and one digit constituted
Two characters.
"S [ao] id" s match and i letters, either a, or is o
"[0-9]" matches any number
"^ [A-z]" matches begin with a lowercase letter row
[^] Matches any character other than the brackets of a character "[^ 0-9]" matches any one non-numeric characters,
"[^ A-z]" represents any one non-lowercase
"^ [^ A-z]" matches the beginning of the line is not a lowercase letter
"^ [^ A-zA-Z]" does not begin to match letters row
\ Escape character. The meaning of the symbols for special cancellation "\. $" Matches the "." At the end of the line
\ {N \} indicate that the previous character appears exactly n times "[0-9] \ {4 \}" matches four numbers,
"[1] [3-8] [0-9] \ {9 \}" matches the phone number
"A \ {3 \}" matches a string of three consecutive letters appear
"[0-9] \ {3 \}" matches contain consecutive three-digit string
\ {N, \} indicate that the previous character appears no less than n times
"[0-9] \ {2 \}" represents two or more numbers.
"[0-9] \ {3, \} [a-z]" matches with a minimum of three consecutive numbers beginning
String
\ {N, m \} indicate that the previous character appears at least n times, most appear
m times
"[A-z] \ {6,8 \}" matches 6-8 lowercase letters.
"Sa \ {1,3 \} i" matches the letter s and i immediately at least one a,
A maximum of three
Here are a few simple Liezi:
(1) to match the date format YYYY-MM-DD "[0-9] \ {4 \} - [0-9] \ {2 \} - [0-9] \ {2 \}"
(2) match the IP address XXX.XXX.XXX "[0-9] \ {1,3 \} \. [0-9] \ {1,3 \} \. [0-9] \ {1,3 \} "
(3) matching Tencent QQ number "[1-9] [0-9] \ {4,9 \}" |
|
|
|