程序媛计划——python正则表达式
生活随笔
收集整理的這篇文章主要介紹了
程序媛计划——python正则表达式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#定義
正則表達式是對字符串操作的一種邏輯公式,通過它我們能篩選過濾出我們需要的內容,如判斷一串數字是否是電話號碼。
?
#原理
先把正則表達式的字符串轉換成 Pattern 對象,接著用這個對象處理文本并得到匹配結果,然后根據結果信息,進行其他的操作?
?
1 #coding:utf-8 2 import re 3 pattern = re.compile(r'hello') #將正則表達式轉化為pattern對象 4 match =pattern.match('hello,cxy!') 5 print match 6 7 if match: 8 print match.group() 9 else: 10 print 'not match' 11 12 pattern2=re.compile(r'Python') 13 match2=pattern2.match('hello cxy') 14 if match2: 15 print match2.group() 16 else: 17 print 'not match'?
轉載于:https://www.cnblogs.com/IcarusYu/p/7533489.html
總結
以上是生活随笔為你收集整理的程序媛计划——python正则表达式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: std:move基本用法和理解
- 下一篇: 对于机器学习中,数据增强