Python_常用的正則表達式處理函數


正則表達式就是用查找字符串的,它能查找規則比較復雜的字符串
反斜杠:正則表達式里面用"\"作為轉義字符。
1 s='<a class="h3" href=""><b>python學習筆記</b></a>'
2 
3 print(re.findall(r'\<a class\=\"h3\" href\=\"\"><b>(.*)\<\/b\>\<\/a\>',s))
里面的一個 r 表示字符串為非轉義的原始字符串,讓編譯器忽略反斜杠,也就是忽略轉義字符。但是這個字符串里沒有反斜杠,所以這個 r 可有可無。

常用的功能函數包括:match、search、findall、sub
1、re.match()函數
函數語法:
re.match(pattern, string, flags=0)
1 def match(pattern, string, flags=0):
2     """Try to apply the pattern at the start of the string, returning
3     a match object, or None if no match was found."""
4     return _compile(pattern, flags).match(string)

函數參數說明:

  • pattern:匹配的正則表達式
  • string:要匹配的字符串
  • flag:標志位,用於控制正則表達式的匹配方式(是否匹配大小寫、多行匹配等)

作用:match()函數只在字符串的開始位置嘗試匹配正則表達式,即從位置0開始匹配。如果匹配成功,則返回一個匹配的對象;如果字符串開始不符合正則表達式,則匹配失敗,函數返回None。

1 import  re
2 test = 'http://news.163.com/17/0624/10/CNMHVBJP0001899N.html'
3 print(re.match(r'http',test)) # <_sre.SRE_Match object; span=(0, 4), match='http'>
4 print(re.match(r'news',test)) # None

2、re.search()函數

函數語法:

1 re.search(pattern, string[, flags])
1 def search(pattern, string, flags=0):
2     """Scan through string looking for a match to the pattern, returning
3     a match object, or None if no match was found."""
4     return _compile(pattern, flags).search(string)

re.search()匹配整個字符串,直到找到第一個匹配的,如果字符串中沒有匹配的,則返回None。

1 import  re
2 test = 'I am a loving child to learn.'
3 print(re.search(r'I',test)) # <_sre.SRE_Match object; span=(0, 1), match='I'>
4 print(re.search(r'learn',test)) # <_sre.SRE_Match object; span=(23, 28), match='learn'>
5 print(re.search(r'alina',test)) # None

3、re.sub()函數

函數語法:

1 re.sub(pattern,repl,string,count,flags)
1 def sub(pattern, repl, string, count=0, flags=0):
2     """Return the string obtained by replacing the leftmost
3     non-overlapping occurrences of the pattern in string by the
4     replacement repl.  repl can be either a string or a callable;
5     if a string, backslash escapes in it are processed.  If it is
6     a callable, it's passed the match object and must return
7     a replacement string to be used."""
8     return _compile(pattern, flags).sub(repl, string, count)

函數參數說明:

  • pattern:匹配的正則表達式
  • repl:替換的字符串
  • String:要被查找替換的原始字符串
  • count:匹配后替換的最大次數,默認0表示途歡所有的匹配

re.sub()函數用於替換字符串中的匹配項。

1 import re
2 test = 'I am a loving child to learn.'
3 print(re.sub(r'child','MMMMM',test)) # 替換字符串,將child 替換成MMMMM

4、re.findall()函數

函數語法:

1 re.findall(pattern,string,flags)
1 def findall(pattern, string, flags=0):
2     """Return a list of all non-overlapping matches in the string.
3 
4     If one or more capturing groups are present in the pattern, return
5     a list of groups; this will be a list of tuples if the pattern
6     has more than one group.
7 
8     Empty matches are included in the result."""
9     return _compile(pattern, flags).findall(string)

re.findall()可以獲取字符串中所有匹配的字符串

1 import re
2 test = '<a href="http://www.educity.cn/zhibo/" target="_blank">直播課堂</a>'
3 print(re.findall(r'<a href="(.*)" target="_blank">(.*)</a>',test)) #[('http://www.educity.cn/zhibo/', '直播課堂')]

在練習正則表達式的時候,用的最多的就是re.findall()函數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM