这是本文档旧的修订版!
查找/usr/share/下,所有mono字体
find . | egrep [mM]ono.*[\.]\(otf\|ttf\)$ find . | egrep "[mM]ono.*\.(otf|ttf)$"
查找包含path=的字符串
rx: "[^"]*path[ ]*=[ ]*[^"]*"
查找日期
rx: \d{4}-\d{2}-\d{2}
字母、数字等,如ABC、abc、123等
如\r \n \t等
就是需要用\转义的字符
$ ( ) * + . [ ? \ ^ { |
注:在代码里,匹配\则需要写成\\\\,当然有的语言支持直接写原表达式
[ABC]: 表示ABC中的任意一个字母
[^ABC]: 表示除了ABC的任意字符
[a-z]: 表示a到z的所有小写字母
. : 除\r \n之外的任意字符
\s: 所有空白字符,包括换行
\S: 所有非空白字符
\w: 字母、数字、下划线,等价于[A-Za-z0-9_]
用于指定给定的组合出现多少次
都可用{n,m}方式表达,如:
* = {0,},+ = {1,},? = {0,1}
在*或+后加?,即非贪婪匹配
str: <a>这首<strong>歌曲</strong>真好听</a>
rx: <.+>
output: <a>这首<strong>歌曲</strong>真好听</a>
rx: <.+?>
output: <a> <strong> </strong> </a>
str: lan qian
rx: (l|qi)an
表示匹配已l或qi开头的,并以an结尾的字符串
用()括起来表示捕获分组,并会缓存匹配
str: <1,21,321>
匹配整个字符串: <\d+,\d+,\d+>
匹配并获取到3个数字: <(\d+),(\d+),(\d+)>
使用()会缓存分组,如果不想缓存,即可用到非捕获元
(?:exp)
str: lan qian
ex: (?:l|qi)an
在(加上?:后,即不缓存此分组
exp1(?=exp2)
匹配后面是exp2的exp1
str: name:hakurei, age:18, weight:45, gender:famale
ex: \w+(?=:\d+)
output: age weight
(?⇐exp1)exp2
匹配前面是exp1的exp2
str: name:hakurei, age:18, weight:45, gender:famale
ex: (?<=age:)\d+
output: 18
exp1(?!exp2)
匹配后面不是exp2的exp1
str: name:hakurei, age:18, weight:45, gender:famale
ex: ([a-zA-Z]+):(?!\d+)
output: name: gender:
exp1(?<!exp2)
匹配前面不是exp1的exp2
str: name:hakurei, age:18, weight:45, gender:famale
ex: ?<!age:[a-zA-Z0-9]+
output: :hakurei :45 :famale