Concepts and features of regular expressions:
Regular expressions are a logical formula for string operations, which is to form a "rule string" using some specific characters defined in advance and combinations of these specific characters.
This "rule string" is used to express a filtering logic for strings. Specify some special syntax to represent character classes, quantity qualifiers and positional relationships, and then use these special syntaxes and general
The characters represent a pattern together, which is the Regular Expression.
Given a regular expression and another string, we can achieve the following purpose:
1. Whether the given string conforms to the filtering logic of the regular expression (called "match");
2. You can get the specific part we want from the string through regular expressions.
Characteristics of regular expressions:
1. Very flexible, logical and functional;
2. It can quickly achieve complex control of strings in extremely simple ways.
3. It is quite obscure for people who are new to come into contact with it.
Since regular expressions mainly apply to text, they are used in various text editors, from the famous editor EditPlus to the large
Large editors such as Microsoft Word and Visual Studio can all use regular expressions to process text content.
1. Regular expressions
1. Definition
(1) Usually used in judgment statements to check whether a certain string satisfies a certain format
(2) Regular expressions are composed of ordinary characters and meta characters
(3) Ordinary characters include upper and lower case letters, numbers, punctuation marks and some other symbols
(4) Metacharacter refers to special characters with special meaning in regular expressions. It can be used to specify the appearance pattern of the leading characters (i.e. characters before the metacharacter) in the target object.
2. Common metacharacters of basic regular expressions (supported tools: grep, egrep, sed, awk)
\: Escape characters, used to cancel the meaning of special symbols, such as: !, \n, $, etc.
^: Match the starting position of the string, for example: a, the, #, [a-z]
$: Match the end position of the string, example: word$, ^$̲ match empty lines
.: Match any character except \n, for example: , g..d
*: Match the previous subexpression 0 or more times, example: good,
[list]: Match a character in the list list, for example: go[ola]d, [abc], [a-z], [a-z0-9], [0-9] to match any one number
[^list]: Match a character in any non-list list, for example: [^0-9], [^A-Z0-9], [^a-z] match any non-lowercase letter
\{n\}: Match the previous subexpression n times, example: go{2}d, '[0-9]{2}' matches two digits
\{n,\}: The subexpression before matching is no less than n times, example: go{2,}d, '[0-9]{2,}' matches two or more digits
\{n,m\}: Match the previous subexpression n to m times, example: go{2,3}d, '[0-9]{2,3}' matches two to three digits
Note: When egrep and awk match {n}, {n,}, {n,m}, do not need to add "\" before "{}"
3. Extended regular expression metacharacters: (supported tools: egrep, awk)
+: Match the previous subexpression more than 1 time, for example: gd+d, will match at least one 0, such as god, good, good, etc.
?: Match the previous subexpression 0 times or times, example: go? d, will match gd or god
(): Take the string in brackets as a whole, for example: g(oo)+d, which will match the whole oo more than 1 time, such as good, goodood, etc.
l: Match the string in the form of OR, for example: g(oo | la)d, match good or glad
2. Regular expression examples
zhangsan1234.@
lisi_3456@
wang wu@
qian@qi@
jc12@
Require:
Username@: The character length is 6 digits or more, and the beginning can only be a letter or _, and the symbols that can be used in the middle are. - # _
Subdomain: maybe uppercase and uppercase letters, numbers, _ -
.Top-level domain name: string length is generally between 2 and 5
This is the end of this article about regular expressions in shell scripts. For more related shell script regular expression content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!