正则表达式
正则表达式是一种强大的字符串操作工具. 它是一种领域特定语言 (DSL), 不管是 Python 还是在大多数现代编程语言中都是作为库存在.
它们主要面向两种任务:
- 验证字符串是否与模式匹配 (例如, 字符串具有电子邮件地址的格式).
- 在字符串中执行替换 (例如将所有大写字母改成小写字母).
特定于领域的语言是高度专业化的迷你编程语言.
正则表达式是一个例子, SQL(用于数据库操作) 是另一个例子.
私有领域特定语言通常用于特定的工业目的.
Python 的正则表达式可以使用 re 模块访问, re 模块是标准库的一部分.
当你定义一个正则表达式, 可以使用 re.match 函数用于确定是否匹配字符串的开始部分. 如果匹配则 match 函数返回表示匹配的对象, 如果不匹配则返回 None.
为了避免在处理正则表达式时出现混淆, 我们将 r 添加到字符串前缀. 该字符串不需要转义任何东西, 使得正则表达式的使用变得更容易.
- from re import match
- msg = r"super"
- if match(msg,"superman!"):
- print("You are True")
- else:
- print("Occur an error! Foolish...")
运行结果:
- >>>
- You are True
- >>>
上面的例子检查模式 super 是否匹配字符串, 如果匹配, 则打印 You are True.
这里的模式是一种简单的单词, 但是有些字符串, 在正则表达式中使用它们时会有特殊的意义.
匹配模式的其他函数有 re.match 和 re.findall.
re.match 在字符串中找到匹配.
re.findall 返回一个包含匹配的列表.
- import re
- string = "Hello python!Hello python!Hello python!"
- pattern = r".python."
- print(re.match(pattern,string))
- print(re.findall(pattern,string))
运行结果:
- >>>
- None
- ['python!', 'python!', 'python!']
- >>>
从上面的示例中, 我们可以得出:
match() 函数是从内容的第一个字符开始匹配, 如果匹配不到, 就得到 None
findall() 函数从全部内容匹配, 如果有多个, 找出所有匹配的
函数 re.finditer 执行与 re.findall 相同的操作, 但它返回一个迭代器, 而不是一个列表.
正则表达式的 search 函数返回一个对象, 包含几个更详细的信息.
此方法包括返回字符串匹配的值, 返回第一次匹配的开始和结束位置, 以及以元组形式返回第一个匹配的开始和结束位置的 span 函数.
- import re
- string = "Hello python!Hello python!Hello python!"
- pattern = r".python."
- match = re.search(pattern,string)
- if match:
- print(match.group())
- print(match.start())
- print(match.end())
- print(match.span())
运行结果:
- >>>
- python!
- 5
- 13
- (5, 13)
- >>>
查找和替换
sub 是正则表达式里非常重要的函数. 表达式:
re.sub(pattern, repl, string, count=0, flags=0)
pattern: 表示正则表达式中的模式字符串;
repl: 被替换的字符串 (既可以是字符串, 也可以是函数);
string: 要被处理的, 要被替换的字符串;
count: 匹配的次数, 默认是全部替换
flags: 具体用处不详
- import re
- string = "Hello python!Hello python!Hello python!"
- pattern = r"python"
- newstr = re.sub(pattern,"Java",string)
- print(newstr)
运行结果:
- >>>
- Hello Java!Hello Java!Hello Java!
- >>>
元字符
元字符使正则表达式比普通字符串方法更强大. 它们允许您创建正则表达式来表示诸如一个或多个数字的匹配.
如果要创建与元字符 (如 $) 匹配的正则表达式, 元字符的存在就会产生问题. 您可以通过在元字符前面添加反斜杠来转义元字符.
但是这可能会导致问题, 因为反斜杠在普通 Python 字符串中也有转义函数. 这可能意味着可能将三个或四个反斜杠排成一行来执行所有转义操作.
为了避免这种情况, 您可以使用一个原始字符串, 它是一个普通字符串, 前面有一个 "r" 前缀.
元字符点, 用来表示匹配除了换行外的任何字符.
- import re
- string1 = "Hello python!Hello python!Hello python!"
- string2 = "pythan,1234587pythoi"
- string3 = r"hello"
- pattern = r"pyth.n"
- match1 = re.search(pattern,string1)
- match2 = re.search(pattern,string2)
- match3 = re.search(pattern,string3)
- if match1:
- print(match1.group())
- print("match 1")
- if match2:
- print(match1.group())
- print("match 2")
- if match3:
- print(match3.group())
- print("match 3")
运行结果:
- >>>
- python
- match 1
- python
- match 2
- >>>
^ 表示匹配开始,$ 表示匹配结束.
- import re
- string1="python"
- string2="pythan,1234587pythoi"
- string3="hello"
- pattern=r"^pyth.n$"
- match1 = re.search(pattern,string1)
- match2 = re.search(pattern,string2)
- match3 = re.search(pattern,string3)
- if match1:
- print(match1.group())
- print("match 1")
- if match2:
- print(match1.group())
- print("match 2")
- if match3:
- print(match3.group())
- print("match 3")
运行结果:
- >>>
- python
- match 1
- >>>
匹配模式 "^pyth.n$" 意味着字符串应该以 pyth 开头, 然后是一个除换行符以外的任何字符, 并以 n 结尾.
"We go through life. We shed our skins. We become ourselves."
"我们经历人生, 我们脱胎换骨. 我们称为自己"-- 帕蒂. 史密斯
来源: https://www.cnblogs.com/dustman/p/10027947.html