Regular expressions for shell learning

grep is a search and filtering tool. Regular expressions are used in grep to find strings that match the pattern. In fact, another important application of regular expressions is to verify whether the user input is legal. For example, when a user submits his email address through a web form, he needs to use a program to verify whether it is a legal email address. This work can be done in the Javascript of the web page. , can also be done in the program behind the website, such as PHP , Perl , Python , Ruby , Java or C , all these languages ​​support regular expressions, it can be said that programming languages ​​that do not support regular expressions are very rare at present. In addition to programming languages, many UNIX commands and tools also support regular expressions, such as grep , vi , sed , awk , emacs , and more. "Regular expressions", like "variables", are a broad concept, not a feature of one tool or programming language.

basic grammar

我们知道C的变量和Shell脚本变量的定义和使用方法很不相同,表达能力也不相同,C的变量有各种类型,而Shell脚本变量都是字符串。同样道理,各种工具和编程语言所使用的正则表达式规范的语法并不相同,表达能力也各不相同,有的正则表达式规范引入很多扩展,能表达更复杂的模式,但各种正则表达式规范的基本概念都是相通的。本节介绍egrep(1)所使用的正则表达式,它大致上符合POSIX正则表达式规范,详见regex(7)(看这个man page对你的英文绝对是很好的锻炼)。希望读者仿照上一节的例子,一边学习语法,一边用egrep命令做实验。

字符类


数量限定符


位置限定符

其它特殊字符



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324843387&siteId=291194637