>>> import re
>>> re.match("c", "abcdef")
>>> re.search("c","abcdef")
<_sre.SRE_Match object at 0x00A9A988>
>>> re.match("c", "cabcdef")
<_sre.SRE_Match object at 0x00A9AB80>
>>> re.search("c","cabcdef")
<_sre.SRE_Match object at 0x00AF1720>
>>> patterm = re.compile("c")
>>> patterm.match("abcdef")
>>> patterm.match("abcdef",1)
>>> patterm.match("abcdef",2)
<_sre.SRE_Match object at 0x00A9AB80>

prog = re.compile(pattern)
result = prog.match(string)

>>> re.split('\W+', 'Words, words, words.')
['Words', 'words', 'words', '']
>>> re.split('(\W+)', 'Words, words, words.')
['Words', ', ', 'words', ', ', 'words', '.', '']
>>> re.split('\W+', 'Words, words, words.', 1)
['Words', 'words, words.']
>>> re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE)

>>> re.split('(\W+)', '...words, words...')
['', '...', 'words', ', ', 'words', '...', '']

>>> re.split("a","bbb")
['bbb']

>>> re.findall("a","bcdef")
[]

>>> re.findall(r"\d+","12a32bc43jf3")
['12', '32', '43', '3']

>>> it = re.finditer(r"\d+","12a32bc43jf3")
>>> for match in it:
print match.group()

I
IGNORECASE

L
LOCALE

M
MULTILINE

S
DOTALL

X
VERBOSE

最后:如果能用字符串的方法,就不要选择正则表达式,因为字符串方法更简单快速。

您可能感兴趣的文章:

  • Python正则表达式re模块详解(建议收藏!)
  • 正则表达式+Python re模块详解
  • Python基础教程之正则表达式基本语法以及re模块
  • python re正则表达式模块(Regular Expression)
  • Python中的re模块之正则表达式模块常用方法