使用演示
import re
print(re.search(r'com', 'http://test.com'))
# <re.Match object; span=(12, 15), match='com'>
print('http://test.com'.find('com'))
# 12
print(re.search(r'.', 'http://test.com'))
# <re.Match object; span=(0, 1), match='h'>
print(re.search(r'test.', 'http://test.com'))
# <re.Match object; span=(7, 12), match='test.'>
print(re.search(r'\.', 'http://test.com'))
# <re.Match object; span=(11, 12), match='.'>
print(re.search(r'\d', 'http://123test.com'))
# <re.Match object; span=(7, 8), match='1'>
print(re.search(r'\d\d', 'http://123test.com'))
# <re.Match object; span=(7, 9), match='12'>
print(re.search(r'[abc]', 'Aab'))
# <re.Match object; span=(1, 2), match='a'>
print(re.search(r'[a-z]', 'Aab'))
# <re.Match object; span=(1, 2), match='a'>
print(re.search(r'ab{3,5}c', 'abbbbbc'))
# <re.Match object; span=(0, 7), match='abbbbbc'>
import re
'逻辑或'
print(re.search(r'python(2|3)', 'python3'))
# <re.Match object; span=(0, 7), match='python1'>
'以...开始;\A同功能,但是限制更强,其他参数限制不了'
print(re.search(r'^python', 'python3'))
# <re.Match object; span=(0, 6), match='python'>
print(re.search(r'^python', 'this_python3'))
# None
'以...结尾;\Z同功能,但是限制更强,其他参数限制不了'
print(re.search(r'python$', 'python3'))
# None
print(re.search(r'python$', 'python'))
# <re.Match object; span=(0, 6), match='python'>
'"\" + 数字 表示要匹配的组个数;当数字是0开头或三位则表示八进制'
print(re.search(r'(python)\1', 'python'))
# None
print(re.search(r'(python)\1', 'pythonpython'))
# <re.Match object; span=(0, 12), match='pythonpython'>
print(re.search(r'.', 'python'))
# <re.Match object; span=(0, 1), match='p'>
print(re.search(r'\.', 'python'))
# None
print(re.search(r'[.]', 'python.py'))
# <re.Match object; span=(6, 7), match='.'>
print(re.search(r'[\n]', 'python.\ny'))
# <re.Match object; span=(7, 8), match='\n'>
print(re.findall(r'[a-z]', 'abcABCabc'))
# ['a', 'b', 'c', 'a', 'b', 'c']
print(re.findall(r'[^a-z]', 'abcABCabc'))
# ['A', 'B', 'C']
print(re.findall(r'^[a-z]', 'abcABCabc'))
# ['a']
'''
* 零次或多次
+ 一次或多次
? 零次或一次
'''
s = '<html><div></div></html>'
'贪婪模式'
print(re.search(r'<.+>', s))
# <re.Match object; span=(0, 24), match='<html><div></div></html>'>
'非贪婪模式'
print(re.search(r'<.+?>', s))
# <re.Match object; span=(0, 6), match='<html>'>
'''
\d 匹配数字0-9
\s 匹配空白字符(包括\t\n\r\f\v)
\w 匹配a-zA-Z0-9以及语言文字和下横线
'''
print(re.findall(r'\w', '你好_(python!)'))
# ['你', '好', '_', 'p', 'y', 't', 'h', 'o', 'n']
import re
'分组'
res = re.search(r'(\w+) (\w+)', 'hi python, haha')
print(res)
# <re.Match object; span=(0, 9), match='hi python'>
print(res.group())
# hi python
print(res.group(1))
# hi
print(res.group(2))
# python
print(res.start())
# 0
print(res.end())
# 9
print(res.span())
# (0, 9)
模式对象
p = re.compile(r'[A-Z]')
print(p.search('Learn Python!'))
# <re.Match object; span=(0, 1), match='L'>
print(p.findall('Learn Python!'))
# ['L', 'P']
网友评论