b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

Python正则表达式

电脑杂谈  发布时间:2020-06-12 00:22:06  来源:网络整理

python正则匹配_正则表达式非贪婪匹配_python 正则匹配

可以确定上述知识点是否是盲目的. 我也是(哭泣和大笑). 让我们用Python代码来消化它们.

# 推荐使用 Python 正则表达式的几个步骤
import re
regex = re.compile(r正则表达式)	# 创建一个 Regex 对象,使用 r 原始字符串不需要转义
regex.match()	# 
regex.search()	# 返回一个 Match 对象,包含被查找字符串中的第一次被匹配的文本 
regex.findall()	# 返回一组字符串列表,包含被查找字符串中的所有匹配
regex.sub()		# 替换字符串,接收两个参数,新字符串和正则表达式
...

简单的例子:

正则表达式非贪婪匹配_python正则匹配_python 正则匹配

>>> import re
>>> regex = re.compile(r\b\w{6}\b)	# 匹配6个字符的单词
>>> regex.search(My phone number is 421-2343-121)
>>> text = regex.search(My phone number is 421-2343-121)
>>> text.group()						# 调用 group() 返回结果
number
>>> regex = re.compile(r0\d{2}-\d{8}|0\d{3}-\d{7})	# 注意分枝条件的使用
>>> text = regex.search(My phone number is 021-76483929)
>>> text.group()
021-76483929
>>> text = regex.search(My phone number is 0132-2384753)
>>> text.group()
0132-2384753 
>>> regex = re.compile(r(0\d{2})-(\d{8}))	# 括号分组的使用
>>> text = regex.search(My phone number is 032-23847533)
>>> text.group(0)
032-23847533
>>> text.group(1)
032
>>> text.group(2)
23847533
>>> regex = re.compile(r(0\d{2}-)?(\d{8}))	# ?之前的分组表示是可选的分组,如果需要匹配真正的?,就使用转义字符\?
>>> text = regex.search(My phone number is 032-23847533)
>>> text.group() 
032-23847533
>>> text = regex.search(My phone number is 23847533)
>>> text.group()
23847533
>>> regex = re.compile(r(Py){3,5})	# Python 默认是贪心,尽可能匹配最长的字符串
>>> text = regex.search(PyPyPyPyPy)
>>> text.group()
PyPyPyPyPy
>>> regex = re.compile(r(Py){3,5}?)	# ? 声明非贪心,尽可能匹配最短的字符串
>>> text = regex.search(PyPyPyPyPy)
>>> text.group()
PyPyPy

其他常规规则可以自行测试. 以下是Python正则表达式的常用方法:

# 这里测试 findall() 以及 sub()
# findall()
>>> regex = re.compile(r0\d{2}-\d{8}|0\d{3}-\d{7})                       
>>> regex.findall(Cell: 021-38294729, Work: 0413-3243243)
[021-38294729, 0413-3243243]
>>> regex = re.compile(rHello \w+)
>>> regex.sub(Hello Python, falkdjfsk Hello c sldfjlksdj Hello java sdfsj)
falkdjfsk Hello Python sldfjlksdj Hello Python sdfsj

编写一个程序,将一段文本复制到剪贴板中,运行该程序,自动找出所有文本的电话号码和电子邮件地址,然后将其复制到剪贴板中供用户使用.

python正则匹配_python 正则匹配_正则表达式非贪婪匹配

思考:

从剪贴板获取文本

匹配所有电话号码和电子邮件地址

将它们粘贴到剪贴板

python正则匹配_python 正则匹配_正则表达式非贪婪匹配

实现:

使用pyperclip模块复制或粘贴字符串,需要使用pip安装pyperclip

创建两个正则表达式,一个与电话匹配,另一个与电子邮件地址匹配

整理匹配的字符串,处理格式,然后发送到剪贴板

正则表达式非贪婪匹配_python 正则匹配_python正则匹配

如果找不到匹配项,请报告错误或提示用户

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# author: Windrivder
# email : windrivder@gmail.com
# date  : 17/03/27 12:58:05
import re, pyperclip
# 匹配电话
phoneRegex = re.compile(r(
    ^(13[0-9]|              # 匹配13开头的电话
    14[5|7]|
    15[0|1|2|3|5|6|7|8|9]|
    18[0|1|2|3|5|6|7|8|9])
    \d{8}$
), re.VERBOSE)           # 如上,传入参数 re.VERBOSE 可以给正则表达式添加注释,详见附录
# 匹配邮件地址
emailRegex = re.compile(r(
    [a-zA-Z0-9._%+-]+           # email-username
    @
    [a-zA-Z0-9.-]+              # domain-name
    (\.[a-zA-Z]{2,4})            # dot-something
), re.VERBOSE)
# 从剪贴板中获取字符串
text = str(pyperclip.paste())
# 存放匹配到的字符串
matches = []
for phone in phoneRegex.findall(text):
    matches.append(phone[0])
for email in emailRegex.findall(text):
    matches.append(email[0])
# 将匹配到的字符串复制到剪贴板
if len(matches) > 0:
    pyperclip.copy(\n.join(matches))
    print(Copied to clipboard:)
    print(\n.join(matches))
else:
    print(No phone or email found.)

关于re.compile()处理选项

在上面的示例中,在re.compile()中传递第二个参数可以实现匹配选项,例如re.VERBOSE可以编写注释;

其他选项: re.IGNORECASE或re.I是忽略匹配的大小写python 正则匹配,re.DOTALL是使点与所有字符匹配

re.compile()的第二个参数只能接受一个值,我们可以使用管道|组合这些选项来绕过这些限制:

someRegex = re.compile(foo, re.I | re.DOTALL | re.VERBOSE)

这样做的目的是希望常规匹配不区分大小写,并且句点字符与换行符匹配python 正则匹配,并且可以添加注释.


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-242370-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 牛伟
        牛伟

        长他人志气灭自己威风

      • 袁涛
        袁涛

        聪明羊期待更优秀的你浮出水面

      热点图片
      拼命载入中...