正则表达式 RE(regular expression)

在线测试工具 -- https://www.baidu.com/link?url=nrvZd0Kt5yD0z5B6AgexhCS61kDf-60_zmyKB-FyDhR8JyDPtWv64BTnveqFUzbn&wd=&eqid=e234c140000283a3000000065b2af7d6

正则表达式百科 -- https://baike.baidu.com/item/正则表达式/1700215?fr=aladdin

Linux中 grep 正则表达式:

*         等价于匹配长度{0,}  

+        等价于匹配长度{1,} 
?         等价于匹配长度{0,1}
.          任何单个字符(\. 字符点)
[list]    找出想要的字符
^   行首                         [^ ]   反向选择
$   行尾

\S       匹配任何可见字符,等价于[^ \f\n\r\t\v]

grep -n    'the'            --color=auto  RE1.txt        有"the"的行
grep -vn   'the'           --color=auto  RE1.txt        没有"the"的行
grep -n     '[^t]he'      --color=auto  RE1.txt       "he"前没有"t"的行,[^]表示反向选择

grep -n  '^Y'      --color=auto  RE1.txt        行首有"Y"的行
grep -n  'Y$'       --color=auto  RE1.txt        行尾有"Y"的行
grep -n  't[hs]e'    --color=auto  RE1.txt      有the或者tse的行


Python中RE:

reg = r'src="([.*\S]*\.jpg)"'

https://docs.python.org/2.7/library/re.html   -- The Python Standard Library — Python 2.7.15 documentation

http://www.runoob.com/regexp/regexp-syntax.html  --简介

https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832260566c26442c671fa489ebc6fe85badda25cd000

python中 re模块

有了准备知识,我们就可以在Python中使用正则表达式了。Python提供re模块,包含所有正则表达式的功能。由于Python的字符串本身也用\转义,所以要特别注意:

s = 'ABC\\-001' # Python的字符串
# 对应的正则表达式字符串变成:
# 'ABC\-001'

因此我们强烈建议使用Python的r前缀,就不用考虑转义的问题了:

s = r'ABC\-001' # Python的字符串
# 对应的正则表达式字符串不变:
# 'ABC\-001'


猜你喜欢

转载自blog.csdn.net/qq_34638161/article/details/80739729
今日推荐