正则表达式

资料地址:https://morvanzhou.github.io/tutorials/python-basic/basic/13-10-regular-expression/

# Regular Expression
import re

pattern1 = 'cat'
pattern2 = 'bird'
string = "dog runs to cat"

# 用正则表达式配对
print(re.search(pattern1, string))
print(re.search(pattern2, string))
# 找到就返回object

ptn_str = "r[au]n"  # 字符串

# 匹配多种可能 使用[] multiple patterns ("run" or "ran")
ptn = r"r[au]n"  # 正则表达式

print(re.search(ptn, string))

print(re.search(r"r[A-Z]n", "dog runs to cat"))
print(re.search(r"r[a-z]n", "dog runs to cat"))
print(re.search(r"r[0-9]n", "dog r2ns to cat"))
print(re.search(r"r[0-9a-z]n", "dog r2ns to cat"))

# 特殊种类匹配

# 反斜杠d代表所有数字形式
# \d:decimal digit
print(re.search(r"r\dn", "run r4n"))

# 反斜杠d代表所有非数字形式\D:any non-decimal digital
print(re.search(r"r\Dn", "run r4n"))

# 空白
# \s:any white space [\t\n\r\f\v] 匹配所有的空格
"""
\t tab键
\n 换行
\r 回车键 enter
\f
\v
"""
print(re.search(r"r\Sn", "r\nn r4n"))

# \S:opposite to \s, any non-while space
print(re.search(r"r\Sn", "r\nn r4n"))

# 所有字母数字和“_”
# \w : {a-zA-Z0-9_}
print(re.search(r"r\wn", "w\nn r4n"))

# \W:opposite to \w (除了小w可以匹配的都能匹配)
print(re.search(r"r\Wn", "w\nn r4n"))

# 空白字符
# \b : empty string (only at the start or end of the word)
print(re.search(r"\bruns\b", "dog runs to cat"))
# \B : empty string (but not at the start or end of a word)
print(re.search(r"\B runs \B", "dog   runs  to cat"))

# 特殊字符 任意字符
# 转义字符
# \\ : match \
print(re.search(r"runs\\", "dog runs\ to cat"))
# . : match anything (except \n)
print(re.search(r"r.n", "r[ns to me"))
print(re.search(r"r.n", "r-ns to me"))

# 句尾句首
# ^:match line beging
print(re.search(r"^dog", "dog runs\ to cat"))
# $:match line ending
print(re.search(r"cat$", "dog runs\ to cat"))

# 是否
# ? : may or may not occur
print(re.search(r"Mon(day)?", "Monday"))
print(re.search(r"Mon(day)?", "Mon"))

# 多行匹配
# multi-line
string = """
dog runs to cat.
I run to dog.
"""

print(re.search(r"^I", string))  # 匹配不到
print(re.search(r"^I", string, flags=re.M))

# 0或者多次
# *:occur 0 or more thing
print(re.search(r"ab*", "a"))  # 可以匹配到
print(re.search(r"ab*", "abbbbb"))

# 1或者多次
# +:occur 1 or more times
print(re.search(r"ab+", "a"))  # None
print(re.search(r"ab+", "abbbbb"))

# 可选次数
# {n,m} : occur n to m times
print(re.search(r"ab{2, 10}", "a"))  
print(re.search(r"ab{2, 10}", "abbbbb"))

# group 组
match = re.search(r"(\d+), Date:(.+)", "ID: 02152632, Date: Feb/12/2017")
print(match.group())
print(match.group(1))  # 第一个括号里面的东西
print(match.group(2))  # 第二个括号里面的东西
# 每一个小括号就是一个组

# 给你的组加一个名字
match = re.search(r"(?P\d+), Date: (?P.+)", "ID: 02152632, Date: Feb/12/2017")
# ?P<名字>
print(match.group("id"))  # 第一个括号里面的东西
print(match.group("date"))  # 第二个括号里面的东西

# 寻找所有匹配
# findall
print(re.findall(r"r[ua]n", "run ran ren"))
# /:or
print(re.findall(r"(run|ran)", "run ran ren"))
# 所匹配出来的东西都是括号里买的了
print(re.findall(r"r(u|a)n", "run ran ren"))

# 替换
# re.sub() replace
print(re.sub(r"r[au]ns", "catches", "dog runs to cat"))

# 分裂
# re.split()
print(re.split(r"[,;\.]", "a;b,c.d;e"))
# . 代表任何东西 \.代表匹配的.点字符

# compile
# 先把匹配形式编译出来,然后在匹配字符串。
compile_re = re.compile(r"r[ua]n")
print(compile_re.search("dog ran to cat"))

结果

<_sre.SRE_Match object; span=(12, 15), match='cat'>
None
<_sre.SRE_Match object; span=(4, 7), match='run'>
None
<_sre.SRE_Match object; span=(4, 7), match='run'>
<_sre.SRE_Match object; span=(4, 7), match='r2n'>
<_sre.SRE_Match object; span=(4, 7), match='r2n'>
<_sre.SRE_Match object; span=(4, 7), match='r4n'>
<_sre.SRE_Match object; span=(0, 3), match='run'>
<_sre.SRE_Match object; span=(4, 7), match='r4n'>
<_sre.SRE_Match object; span=(4, 7), match='r4n'>
<_sre.SRE_Match object; span=(4, 7), match='r4n'>
None
<_sre.SRE_Match object; span=(4, 8), match='runs'>
<_sre.SRE_Match object; span=(5, 11), match=' runs '>
<_sre.SRE_Match object; span=(4, 9), match='runs\\'>
<_sre.SRE_Match object; span=(0, 3), match='r[n'>
<_sre.SRE_Match object; span=(0, 3), match='r-n'>
<_sre.SRE_Match object; span=(0, 3), match='dog'>
<_sre.SRE_Match object; span=(13, 16), match='cat'>
<_sre.SRE_Match object; span=(0, 6), match='Monday'>
<_sre.SRE_Match object; span=(0, 3), match='Mon'>
None
<_sre.SRE_Match object; span=(18, 19), match='I'>
<_sre.SRE_Match object; span=(0, 1), match='a'>
<_sre.SRE_Match object; span=(0, 6), match='abbbbb'>
None
<_sre.SRE_Match object; span=(0, 6), match='abbbbb'>
None
None
02152632, Date: Feb/12/2017
02152632
 Feb/12/2017
02152632
Feb/12/2017
['run', 'ran']
['run', 'ran']
['u', 'a']
dog catches to cat
['a', 'b', 'c', 'd', 'e']
<_sre.SRE_Match object; span=(4, 7), match='ran'>

上面链接里买有个小抄
可以看一看

向莫烦老师致敬

你可能感兴趣的:(正则表达式)