正则表达式常用的方法:
match
用法介绍:
Try to apply the pattern at the start of the string, returning
a Match object, or None if no match was found:
import re
result='hello world,哈喽'
ret=re.match('hello',result)
print(ret.group())
**output**
hello
seach
用法介绍:
“”“Scan through string looking for a match to the pattern, returninga Match object, or None if no match was found.”""
import re
result='hello world,哈喽'
ret=re.search('world',result)
print(ret.group())
**output**
world
split
用法介绍:
“”“Split the source string by the occurrences of the pattern,
returning a list containing the resulting substrings. If
capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resultinglist. If maxsplit is nonzero, at most maxsplit splits occur,and the remainder of the string is returned as the final elementof the list.”""
import re
result='hello world,哈喽'
ret=re.split(' |,',result)
print(ret)
**output**
['hello', 'world', '哈喽']
findall
用法介绍:
“”“Return a list of all non-overlapping matches in the string.
If one or more capturing groups are present in the pattern, returna list of groups; this will be a list of tuples if the patternhas more than one group.Empty matches are included in the result.”""
import re
result='hello world,哈喽'
ret=re.findall('world|哈喽',result)
print(ret)
**output**
['world', '哈喽']