import re
print(re.search(r'Fishc', 'I love Fishc.com!'))
<re.Match object; span=(7, 12), match='Fishc'> # 第七到第十二
print('I love Fishc.com!'.find('Fishc'))
7
import re
print(re.search(r'.', 'I love Fishc.com!')) # 点表示任何字符,所以是第一个字符
print(re.search(r'Fish.', 'I love Fishc.com!'))
print(re.search(r'\.', 'I love Fishc.com!'))
<re.Match object; span=(0, 1), match='I'>
<re.Match object; span=(7, 12), match='Fishc'>
<re.Match object; span=(12, 13), match='.'>
import re
print(re.search(r'\d', 'I love 123Fishc.com!')) # 匹配数字
print(re.search(r'\d\d\d', 'I love 123Fishc.com!'))
<re.Match object; span=(7, 8), match='1'>
<re.Match object; span=(7, 10), match='123'>
import re
print(re.search(r'\d\d\d\.\d\d\d\.\d\d\d\.\d\d\d', '192.168.111.123')) # 匹配IP地址
<re.Match object; span=(0, 15), match='192.168.111.123'>
import re
print(re.search(r'\d\d\d\.\d\d\d\.\d\d\d\.\d\d\d', '192.168.1.1')) # 匹配IP地址
None
import re
print(re.search(r'[aeiou]', 'I love Fishc.com!'))
<re.Match object; span=(3, 4), match='o'>
import re
print(re.search(r'[aeiouAEIOU]', 'I love Fishc.com!'))
<re.Match object; span=(0, 1), match='I'>
import re
print(re.search(r'[a-z]', 'I love Fishc.com!'))
<re.Match object; span=(2, 3), match='l'>
import re
print(re.search(r'[0-9]', 'I love 123Fishc.com!'))
print(re.search(r'[2-9]', 'I love 123Fishc.com!'))
<re.Match object; span=(7, 8), match='1'>
<re.Match object; span=(8, 9), match='2'>
import re
print(re.search(r'ab{3}c', 'abbbc')) # 大括号里面是重复的次数
print(re.search(r'ab{3}c', 'abbbbbc'))
print(re.search(r'ab{3,10}c', 'abbbbbc')) # 3到10次
<re.Match object; span=(0, 5), match='abbbc'>
None
<re.Match object; span=(0, 7), match='abbbbbc'>
import re
print(re.search(r'[0-255]', '188')) # 0到2,两个5
print(re.search(r'[0-2][0-5][0-5]', '188'))
print(re.search(r'[01]\d\d|2[0-4]\d|25[0-5]', '188')) #01x or 24x or 25x
<re.Match object; span=(0, 1), match='1'>
None
<re.Match object; span=(0, 3), match='188'>
import re
print(re.search(r'(([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])\.){3}([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])', '192.168.1.1'))
<re.Match object; span=(0, 11), match='192.168.1.1'>
print(re.search(r"Fish(C|D)", 'FishC'))
print(re.search(r"Fish(C|D)", 'FishD'))
print(re.search(r"Fish(C|D)", 'FishE'))
<re.Match object; span=(0, 5), match='FishC'>
<re.Match object; span=(0, 5), match='FishD'>
None
import re
print(re.search(r"^FishC", 'I love FishC.com!'))
print(re.search(r"^FishC", 'FishC.com!'))
None
<re.Match object; span=(0, 5), match='FishC'>
import re
print(re.search(r"FishC$", 'I love FishC.com!'))
print(re.search(r"FishC$", 'I love FishC'))
None
<re.Match object; span=(7, 12), match='FishC'>
\
() 子组
import re
print(re.search(r"(FishC)\1", 'FishC.com!'))
print(re.search(r"(FishC)\1", 'FishCFishC!'))
None
<re.Match object; span=(0, 10), match='FishCFishC'>
import re
print(re.search(r"(FishC)\060", 'FishCFishC0'))
<re.Match object; span=(5, 11), match='FishC0'>
import re
print(re.search(r"(FishC)\141", 'FishCFishCa')) # 三位数表示八进制
<re.Match object; span=(5, 11), match='FishCa'>
import re
print(re.search(r".", 'FishCFishCa'))
print(re.search(r"\.", 'FishCFishCa'))
print(re.search(r"[.]", 'FishC.com'))
print(re.findall(r"[a-z]", 'FishC.com'))
<re.Match object; span=(0, 1), match='F'>
None
<re.Match object; span=(5, 6), match='.'>
['i', 's', 'h', 'c', 'o', 'm']
import re
print(re.findall(r"[\n]", 'FishC.com\n'))
print(re.findall(r"[^a-z]", 'FishC.com\n')) # 取反
print(re.findall(r"[a-z^]", 'FishC.com\n'))
['\n']
['F', 'C', '.', '\n']
['i', 's', 'h', 'c', 'o', 'm']
import re
print(re.search(r"FishC{3}", 'FishCCCCCC')) # 匹配C三次
print(re.search(r"FishC{3,4}", 'FishCCCCCC')) # 匹配C四次
print(re.search(r"(FishC){3}", 'FishCFishCFishC')) # 匹配FishC三次
print(re.search(r"(FishC){1,4}", 'FishCFishCFishC')) # 匹配1到4次,这里只有三次
<re.Match object; span=(0, 7), match='FishCCC'>
<re.Match object; span=(0, 8), match='FishCCCC'>
<re.Match object; span=(0, 15), match='FishCFishCFishC'>
<re.Match object; span=(0, 15), match='FishCFishCFishC'>
表示匹配前面的子表达式零次或多次。等价于{0,}
表示匹配前面的子表达式一次或多次,等价于{1,}
表示匹配前面的子表达式一次或1次,等价于{0,1}
import re
print(re.search(r"(FishC){1,5}", 'FishCFishCFishC'))
s = "I love FishC.com "
print(re.search(r"<.+>", s)) # 贪婪模式
print(re.search(r"<.+?>", s))
<re.Match object; span=(0, 15), match='FishCFishCFishC'>
<re.Match object; span=(0, 44), match='I love FishC.com '>
<re.Match object; span=(0, 6), match=''>
正则表达式里面不能随便加空格
字符 | 含义 |
---|---|
. | 表示匹配除了换行符外的任何字符。 注:通过设置re.DOTALL标志可以使.匹配任何字符(包括换行符) |
| | A|B表示匹配正则表达式A或者B |
^ | 1.(脱字符撇皮输入字符串的开始位置);2.如果设置了re.MULTILINE标志,^也匹配换行符之后的位置 |
$ | 1.匹配输入字符串的结束位置;2.如果设置了re.MULTILINE标志 ,$也匹配换行符之前的位置 |