码迷,mamicode.com
首页 > 系统相关 > 详细

linux文本处理之常用正则表达式整理

时间:2017-03-22 23:43:06      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:linux   正则   

^ 行首。


$行尾。


. 除换行符之外任意一个字符。


* 前面的字符重复0到多次。


.*用于匹配所有字符。


[]定义一组字符,匹配组内任一一个字符(注意!只是组内的一个!)


[^] 对字符组内的字符做取反操作(不包涵组内任一一个字符)


^[] 组内字符串开头的行。


^[^] 非组内字符串开头的行。


[a-z]  匹配一个小写字母。


[A-Z] 匹配一个大写字母。


[a-Z] 匹配一个小写和大写字母。


[0-9] 匹配一个0-9的数字。


\< 匹配单词头

\> 匹配单词尾

(单词在正则中的定义,就是以空格或者其他特殊字符做分割,连续的字符串会被当做单词。)


拓展正则:

? 前字符匹配0个或1个


+前字符匹配1个或多个


|逻辑或,例如abc|def abc或def


()小括号,用于分组,例:a(bc|de)f

意思就是abcf或adef。


s{n}  s重复了n次。


s{n,}  s至少重复了n次,甚至更多次。


s{n,m}  s至少重复了n次,最多重复m次。


posix定义的字符分类:


[:alnum:] Alphanumeric characters.

匹配范围为 [a-zA-Z0-9]

[:alpha:] Alphabetic characters.

匹配范围为 [a-zA-Z]

[:blank:] Space or tab characters.

匹配范围为 空格和TAB键

[:cntrl:] Control characters.

匹配控制键 例如 ^M 要按 ctrl+v 再按回车 才能输出

[:digit:] Numeric characters.

匹配所有数字 [0-9]

[:graph:] Characters that are both printable and visible. (A space is print-

able, but not visible, while an a is both.)

匹配所有可见字符 但不包含空格和TAB 就是你在文本文档中按键盘上能用眼睛观察到的所有符号

[:lower:] Lower-case alphabetic characters.

小写 [a-z]

[:print:] Printable characters (characters that are not control characters.)

匹配所有可见字符 包括空格和TAB

能打印到纸上的所有符号

[:punct:] Punctuation characters (characters that are not letter, digits, con-

trol characters, or space characters).

特殊输入符号 +-=)(*&^%$#@!~`|\"‘{}[]:;?/>.<,

注意它不包含空格和TAB

这个集合不等于^[a-zA-Z0-9]

[:space:] Space characters (such as space, tab, and formfeed, to name a few).

[:upper:] Upper-case alphabetic characters.

大写 [A-Z]

[:xdigit:] Characters that are hexadecimal digits.

16进制数 [0-f]

使用方法:

[root@seker ~]# grep --color ‘[[:alnum:]]‘ /etc/passwd


本文出自 “reBiRTH” 博客,请务必保留此出处http://suhaozhi.blog.51cto.com/7272298/1909379

linux文本处理之常用正则表达式整理

标签:linux   正则   

原文地址:http://suhaozhi.blog.51cto.com/7272298/1909379

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!