By Shell scripting application (a) , Shell Script applications (2) , Shell scripting application (c) which several blog posts, we have mastered the rules of writing Shell scripts and specific application of various statements, but the actual production environment, Shell script expressions generally positive, text-processing tools combine. We come to know what "regular expression (RE)".
Regular expressions overview
1. The definition of regular expressions
Regular expressions also known as regular expressions, regular expressions. Code often abbreviated as regex, regexp or RE. Regular expressions are used to describe the use of a single string, the string line with a series of matching syntax rules. Simply put, the regular expression is a way to match the string, through some special symbols, quick search, delete, replace a specific string.
正则表达式作为一个模板,将某个字符模式与所搜索的字符串相匹配。其中由两部分组成: 1.普通字符:包括大小写字母、数字、标点符号及一些其他符号; 2. 元符号:是指那些在正则表达式中具有特殊意义的专用字符。
Regular expressions are generally used for scripting and text editor. Many text processor and programming languages support regular expressions. Regular expressions have a very powerful text-matching capabilities to quickly and efficiently handle text in a text sea.
2. The use of regular expressions
Regular Expressions for system administrators is very important, the system is running will generate a lot of information, some of this information is very important, and some are for information only high school. As an administrator if the direct data to see so much information, not quickly locate important information. Such as "user account login failed" and "service failed to start" and other important information. This is the regular expression can quickly extract information "problematic", this way, can the operation and maintenance work easier and convenient.
At present, many software also supports regular expressions, the most common is the mail server. On the Internet, junk advertising, mail, etc. will cause network congestion, if the server will reject these issues ahead of time, then the client will reduce a lot of unnecessary bandwidth consumption.
正则表达式的字符串根据不同的严谨程度与功能分为:
1.基本正则表达式;
2.扩展正则表达式。
First, the basic regular expressions
In the Linux system common file processing tool grep and sed to support basic regular expressions, basic regular expressions to grep command as an example
Basis regular expression examples:
1) Find contain "the" row
[root@localhost ~]# grep -n 'the' test.txt
grep命令的选项有: * -n:表示显示行号; * -i:表示不区分大小写; * -v:表示反向查找。
2) Find that does not contain "the" row
[root@localhost ~]# grep -vn 'the' test.txt
3) Find begin sh, ending rt, i is the intermediate character or o
[root@localhost ~]# grep -n 'sh[io]rt' test.txt
4) not preceded by a query string oo w,
[root@localhost ~]# grep -n '[^w]oo' test.txt
5) preceding the query string oo not lowercase
[root@localhost ~]# grep -n '[^a-z]oo' test.txt
6) ^ Matches start with a character. At the beginning of the query string
[root@localhost ~]# grep -n '^the' test.txt
7) [^] matches any character that is not included. The query string does not begin with a letter
[root@localhost ~]# grep -n '^[^a-zA-Z]' test.txt
8) $ matches the end of the line to a character. Query to the end. Strings
[root@localhost ~]# grep -n '\.$' test.txt //其中小数点“.”具有特殊意义,所以需要使用转义字符“\”将具有特殊意义的字符转化为普通字符
9). "" Matches any character other \ r \ outside n. Line contains two query character between w and d
[root@localhost ~]# grep -n 'w..d' test.txt
10) comprising a continuous line query letter o
[root@localhost ~]# grep -n 'ooo*' test.txt //“*”表示的是重复零个或多个前面的单字符
11) a query to the intermediate line begins w d comprises at least one end of the o
[root@localhost ~]# grep -n 'woo*d' test.txt
12) w d query beginning end, the middle row of characters dispensable
[root@localhost ~]# grep -n 'w.*d' test.txt
13) {n} n times matching determination. Queries line contains two o's
[root@localhost ~]# grep -n 'o\{2\}' test.txt //“{}”是特殊字符需要用“\”转义
14) to the end of the query beginning w d, 2 to 5 comprising an intermediate rows o
[root@localhost ~]# grep -n 'wo\{2,5\}' test.txt // {n,m}最少匹配n次且最多m次
15) ends in a query beginning w d, comprising two or more intermediate rows o
[root@localhost ~]# grep -n 'wo\{2,\}' test.txt // {n,}至少匹配n次
The basis of common regular expression metacharacters summary
Second, the extended regular expression
Typically the foundation will use the regular expression would have been sufficient, but in order to simplify the instructions, require the use of a wider range of extended regular expressions.
In the Linux system common text processing tool egrep and awk support extended regular expression usage, egrep command grep command is similar.
Extended regular expressions common metacharacters summary
Regular expressions are brief so much, and so have needs, and will be updated in real time!