~ Using regular Expressions
- The RE module provides an interface to the regular expression engine that allows you to compile restring into objects and use them for matching.
#如果经常使用, it is recommended to compile compile
Example:
>>> r1=r ' ^\d{3,4}-?\d{8} ' >>> Re.findall (R1, ' 010-12345678 ') [' 010-12345678 ']>>> p_tel= Re.compile (R1) #正则编译, becomes an object, using the Complie method in the RE module to compile;>>> R1 to the compiler. Sre_pattern object at 0x7f2e016173d0> #正则对象 >>> p_tel.findall (' 01035689125 ') [' 01035689125 ']
#编译的正则比未编译的正则效率高
----------------------------------------------------------------------------------------------------------
-re.compile () also accepts an optional Flag Parameters , focus on re. I
-Used to implement different special functions and grammatical changes
Example:
>>> csvt_re=re.compile (R ' CSVT ', re. I) #在编译过程中, add the regular attribute re. I (case insensitive) >>> csvt_re.findall (' csvtcsvt ') [' csvt ', ' CSVT ']
#注意: Add ' r ' to the string before the backslash will not be processed in any special way
----------------------------------------------------------------------------------------------------------
To perform a match:
-The ' Regexobject ' instance has some methods and properties, and the complete list is available in the Python Library Reference
<
| Method/Property |
Role |
| Match () |
Determines if re is matched at the beginning of the string |
| Search () |
Scan the string to find the location of the re match |
| FindAll () |
Find all the substrings that the re matches and return it as a list |
| Finditer () |
Find all the substrings that the re matches and return it as an iterator |
If no match is reached, match () and search will return none
If successful, a ' Matchobject ' instance is returned.
Example:
Use match
>>> csvt_re=re.compile (R ' CSVT ', re. I) >>> csvt_re.match (' csvt hello ') <_sre. Sre_match object at 0x7f2e01620920> #匹配成功. return macthobject>>> csvt_re.match (' hello ') #匹配失败, return empty >>> csvt_re.match (' Hello csvt ') #返回为空, Note that match matches the position where the string starts
If you use Search
>>> csvt_re.search (' Hello csvt ') <_sre. Sre_match object at 0x7f2e01620988>>>> csvt_re.search (' Hello csvt ') <_sre. Sre_match object at 0x7f2e01620920> #注意此处为不区分大小写 and full table scan
Using FindAll
>>> csvt_re.findall (' Hello csvt hello csvt csvt ') [' csvt ', ' csvt ', ' CSVT '] #返回为列表
Using Finditer
>>> csvt_re.finditer (' Hello csvt hello csvt csvt ') <callable-iterator object at 0x7f2e01634350># Object returned as iterator >>> csvt_re.findall (' CSVT ') [' CSVT ']
Match () Matchobject instance method
| Method/Property |
Role |
| Group () |
Returns the string that is matched by the RE |
| Start () |
Returns the position where the match started |
| End () |
Returns the position where the match ended |
| Span () |
Returns a tuple containing the location of a match (start, end) |
Group (): return Object
>>> csvt_re=re.compile (R ' CSVT ', re. I) >>> x =csvt_re.match (' Csvt,hello ') >>> x<_sre. Sre_match object at 0x7fa4f527f920>>>> x.group () ' CSVT '
----------------------------------------------------------------------------------------------------------
-Module-level functions
The-re module also provides top-level function calls to match (), search (), subn (), Split (), FindAll (), etc.
Sub, SUBN: Regular replacement
Example:
#单纯的字符串替换 >>> kong = ' Hello Nihao ' >>> kong.replace (' Nihao ', ' word ') ' Hello word ' #使用正则 >>> rs = r ' N...O ' >>> kong = ' Hello Nihao naabo nccco ' >>> re.sub (RS, ' BBB ', Kong) ' Hello BBB bbb bbb ' #匹配替换所有 >> > re.subn (RS, ' BBB ', Kong) (' Hello bbb bbb BBB ', 3) difference between #sub and subn, replacement count
Split (): Regular cut
#字符切割 >>> ip = ' 1.2.3.4 ' >>> ip.split ('. ') [' 1 ', ' 2 ', ' 3 ', ' 4 ']>>> s = ' 123+456-789*000 ' >>> re.split (R ' [\+\-\*\/] ', s) [' 123 ', ' 456 ', ' 789 ', ' 000 ']
#查看更多正则函数:
>>> Dir (re)
#查看帮助:
Help (Re.error)
This article from "thinking More than technology" blog, declined reprint!
Python Basic Learning Regular Expression 2 (use)