python正则表达式match,search,find的使用方法
生活随笔
收集整理的這篇文章主要介紹了
python正则表达式match,search,find的使用方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.使用match()匹配字符串:
match()函數(shù)試圖從字符串的開始部分對模式進行匹配,
匹配對象的group()方法能夠用于顯示那個成功的匹配。
>>>import re >>>m=re.match('foo', 'fooid').group() 'foo' >>>n=re.match('foo', 'idfooid').group() AttributeError:'NoneType' object has no attribute 'group'2.使用search()函數(shù)匹配字符串
search()的工作方式和match()完全一樣,不同之處在于search()會用它的字符串參數(shù),
在任意位置對給定的正則表達式模式搜索第一次出現(xiàn)的匹配情況。
如果匹配成功,就會返回一個匹配對象。
>>>import re >>>m=re.search('foo', 'idfoo').group() >>>m 'foo'?
轉載于:https://www.cnblogs.com/lmt921108/p/8024593.html
總結
以上是生活随笔為你收集整理的python正则表达式match,search,find的使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spark Mllib里的如何对单个数据
- 下一篇: PRISM ‘VS100COMNTOOL