python——re模块:元字符之. ^ $ * + ? {}

元字符:
. :通配符,除了换行符(\n)以外都可以匹配
^ :必须在字符串的开头匹配
$ :必须在字符串的结尾匹配
* :按紧挨着的字符去重复,包含(0,+00),*前面的字符,在后面的字符串中就算没有也能匹配
+ :按紧挨着的字符去重复,包含(1,+00),+前面的字符,在后面的字符串中至少要有一个才能匹配,*和+都是贪婪匹配,因为都是到+00,所以后面有多少都会匹配出来
? :按紧挨着的字符去重复,包含(0,1)
{} :{0,}==*  {1,}==+  {0,1}==?
注意:前面的*,+,?等都是贪婪匹配,也就是尽可能匹配,后面加?使其变成惰性匹配

>>> import re
>>> re.findall("a..x","adsfaeyxsklg")
['aeyx']
>>> re.findall("a...x","adsfaeyxsklg")
[]
>>> re.findall("^a..x","adsfaeyxsklg")
[]
>>> re.findall("^a..x","adsxaeyxsklg")
['adsx']
>>> re.findall("a..x$","adsxaeyxsklg")
[]
>>> re.findall("a..x$","adsxaeyxsklgarrx")
['arrx']
>>> re.findall("a..x$","adsxaeyxsklgarrx$")
[]
>>> re.findall("d*","sfdgsdfdddddddddhfjlkjsfd")
['', '', 'd', '', '', 'd', '', 'ddddddddd', '', '', '', '', '', '', '', '', 'd', '']
>>> re.findall("^d*","dddddddlksdfgjdddddd")
['ddddddd']
>>> re.findall("alex*","asdhfalexxx")
['alexxx']
>>> re.findall("alex+","asdhfalexxx")
['alexxx']
>>> re.findall("alex*","asdhfale")
['ale']
>>> re.findall("alex+","asdhfale")
[]
>>> re.findall("alex?","asdhfalexxx")
['alex']
>>> re.findall("alex?","asdhfale")
['ale']
>>> re.findall("alex{6}","asdhfalexx")
[]
>>> re.findall("alex{6}","asdhfalexxxxxx")
['alexxxxxx']
>>> re.findall("alex{0,6}","asdhfalexx")
['alexx']
>>> re.findall("alex{0,1}","asdhfalexx")
['alex']
>>> re.findall("alex*?","asdhfalexxxxxx")
['ale']
>>> re.findall("alex+?","asdhfalexxxxxx")
['alex']

 

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值