动手学正则表达式(含Python代码实践)(下)

简介: 动手学正则表达式(含Python代码实践)(下)

5. 位置匹配


example 1:字符边界


微信图片_20220524141116.png


text="The cat scattered his food all over the room."
get_res(r"cat",text)
get_res(r"\bcat\b",text)


findall res: ['cat', 'cat']
search res: <re.Match object; span=(4, 7), match='cat'>
findall res: ['cat']
search res: <re.Match object; span=(4, 7), match='cat'>


example 2:"\B"边界


微信图片_20220524141135.png


text="Please enter the nine-digit id as it appears on your color - coded pass-key."
get_res(r'\B-\B',text)
print(text[55:63])
get_res(r'\b-\b',text)


findall res: ['-']
search res: <re.Match object; span=(59, 60), match='-'>
lor - co
findall res: ['-', '-']
search res: <re.Match object; span=(21, 22), match='-'>


example 3:字符串边界


微信图片_20220524141150.png


text="<?xml version=”1.0” encoding=”UTF-8” ?> <wsdl:definitions targetNamespace=”http://tips.cf” xmlns:impl=”http://tips.cf” xmlns:intf=”http://tips.cf” xmlns:apachesoap=”http://xml.apache.org/xml-soap”"
get_res(r'^\s*<\?xml.*\?>',text)


findall res: ['<?xml version=”1.0” encoding=”UTF-8” ?>']
search res: <re.Match object; span=(0, 39), match='<?xml version=”1.0” encoding=”UTF-8” ?>'>


6. 使用子表达式



example 1:子表达式


微信图片_20220524141205.png


text="Hello, my name is Ben&nbsp;Forta, and I am the author of books on SQL, ColdFusion, WAP,\
Windows&nbsp;&nbsp;2000, and other subjects."
get_res(r'&nbsp;{2,}',text)
get_res(r'(&nbsp;){2,}',text)


findall res: []
search res: None
findall res: ['&nbsp;']
search res: <re.Match object; span=(94, 106), match='&nbsp;&nbsp;'>


example 2:子表达式


text="Pinging hog.forta.com [12.159.46.200] with [12.159.89.200] 32 bytes of data:"
#findall中含有“()”时,只表示出子表达式特定位置的字符,见下文结果
get_res(r"(\d{1,3}\.){3}\d{1,3}",text)


findall res: ['46.', '89.']
search res: <re.Match object; span=(23, 36), match='12.159.46.200'>


example 3:子表达式


text='ID: 042 \
SEX: M \
DOB: 1967-08-17 \
Status: Active'
get_res(r"19|20\d{2}",text)
get_res(r"(19|20)\d{2}",text)


findall res: ['19']
search res: <re.Match object; span=(20, 22), match='19'>
findall res: ['19']
search res: <re.Match object; span=(20, 24), match='1967'>


7.使用回溯



「回溯引用允许正则表达式模式引用前面的匹配结果。可以把回溯引用想象成一个变量。」

example 1:回溯


text='This is a block of of text, several words here are are repeated, and and they should not be.'
get_res(r"[ ]+(\w+)[ ]+\1",text)#\1 就是引用(\w+)的内容,表示第1个子表达式


findall res: ['of', 'are', 'and']
search res: <re.Match object; span=(15, 21), match=' of of'>


example 2: 回溯


text="<BODY> \
<H1>Welcome to my Homepage</H1> \
Content is divided into two sections:<BR> <H2>ColdFusion</H2> \
Information about Macromedia ColdFusion. <H2>Wireless</H2> \
Information about Bluetooth, 802.11, and more. <H2>This is not valid HTML</H3> \
</BODY>"
get_res(r"<[hH]([1-6])>.*?</[hH]\1>",text)


findall res: ['1', '2', '2']
search res: <re.Match object; span=(7, 38), match='<H1>Welcome to my Homepage</H1>'>


8. 前后查找



前后查找(lookaround)对某一位置的前、后内容进行查找。


example 1:向前查找


微信图片_20220524141241.png


text='http://www.forta.com/\n\
https://mail.forta.com/\n\
ftp://ftp.forta.com/\n'
get_res(r".+(?=:)",text)


findall res: ['http', 'https', 'ftp']
search res: <re.Match object; span=(0, 4), match='http'>


example 2:向后查找


微信图片_20220524141252.png


text='ABC01: $23.45\n\
HGG42: $5.31\n\
CFMX1: $899.00 XTC99: $69.96\n\
Total items found: 4'
get_res(r"(?<=\$)[0-9.]+",text)


findall res: ['23.45', '5.31', '899.00', '69.96']
search res: <re.Match object; span=(8, 13), match='23.45'>


example 3:向前向后查找


text='<HEAD>\n\
<TITLE>Ben Forta’s Homepage</TITLE>\n\
</HEAD>'
get_res(r"(?<=<[tT][iI][tT][lL][eE]>).*(?=</[tT][iI][tT][lL][eE]>)",text)


findall res: ['Ben Forta’s Homepage']
search res: <re.Match object; span=(14, 34), match='Ben Forta’s Homepage'>


example 4:负向查找


微信图片_20220524141307.png


text="I paid $30 for 100 apples, 50 oranges, and 60 pears. I saved $5 on this order."
get_res(r"(?<!\$)\d+",text)


findall res: ['0', '100', '50', '60']
search res: <re.Match object; span=(9, 10), match='0'>


9. 嵌入条件



微信图片_20220524141321.png


example 1:(?(backreference)true-regex)

text='<!-- Nav bar -->\n\
<TD>\n\
<A HREF=”/home”><IMG SRC=”/images/home.gif”></A>\n\
<IMG SRC=”/images/spacer.gif”>\n\
<A HREF=”/search”><IMG SRC=”/images/search.gif”></A>\n\
<IMG SRC=”/images/spacer.gif”>\n\
<A HREF=”/help”><IMG SRC=”/images/help.gif”></A> </TD>'
get_res(r"(<[Aa]\s+[^>]+>\s*)?<[Ii][Mm][Gg]\s+[^>]+>(?(1)\s*</[Aa]>)",text)


findall res: ['<A HREF=”/home”>', '', '<A HREF=”/search”>', '', '<A HREF=”/help”>']
search res: <re.Match object; span=(22, 70), match='<A HREF=”/home”><IMG SRC=”/images/home.gif”></A>'>


example 2:(?(backreference)true-regex|false-regex)


text='123-456-7890\n\
(123)456-7890\n\
(123)-456-7890\n\
(123-456-7890\n\
1234567890\n\
123 456 7890'
get_res(r"(\()?\d{3}(?(1)\)|-)\d{3}-\d{4}",text)


findall res: ['', '(', '']
search res: <re.Match object; span=(0, 12), match='123-456-7890'>


代码


以上的东西我已经整理好了:


一文入坑正则表达式代码(https://github.com/weizaiff/regexp)

相关文章
|
4天前
|
数据处理 Python
从零开始学迭代器生成器:打造高效、易读的Python代码
从零开始学迭代器生成器:打造高效、易读的Python代码
|
4天前
|
数据挖掘 数据处理 Python
Python编程入门:从基础到实践
【6月更文挑战第26天】这篇文章引导读者逐步学习Python编程,从基础语法如变量、数据类型(整数、浮点数、字符串)到条件语句、循环(if/for/while),再到函数定义和模块导入。通过实例展示了Python在文本处理、数据分析(使用pandas)和Web开发(使用Flask)的应用。学习Python能为初学者开启更广阔的技术领域,如面向对象编程、并发和网络编程等。
|
4天前
|
机器学习/深度学习 算法 索引
Python常用极简代码
Python常用极简代码
28 5
|
4天前
|
Python
Python实用案例代码详解
Python实用案例代码详解
12 2
|
2天前
|
测试技术
Appium+python自动化(三十九)-Appium自动化测试框架综合实践 - 代码实现(超详解)
Appium+python自动化(三十九)-Appium自动化测试框架综合实践 - 代码实现(超详解)
|
3天前
|
程序员 API 计算机视觉
技术经验解读:【python自动化】02.pywin32库自动操作键鼠(保姆级代码注释)
技术经验解读:【python自动化】02.pywin32库自动操作键鼠(保姆级代码注释)
|
4天前
|
Python
Python正则表达式详解:掌握文本匹配的魔法
Python正则表达式详解:掌握文本匹配的魔法
|
4天前
|
Python
python re 正则表达式库的使用
python re 正则表达式库的使用
4 0
|
4天前
|
人工智能 数据挖掘 大数据
538个代码示例!麻省理工教授的Python程序设计+人工智能案例实践
Python简单易学,且提供了丰富的第三方库,可以用较少的代码完成较多的工作,使开发者能够专注于如何解决问题而只花较少的时间去考虑如何编程。 此外,Python还具有免费开源、跨平台、面向对象、胶水语言等优点,在系统编程、图形界面开发、科学计算、Web开发、数据分析、人工智能等方面有广泛应用。 尤其是在数据分析和人工智能方面,Python已成为最受开发者欢迎的编程语言之一,不仅大量计算机专业人员选择使用Python进行快速开发,许多非计算机专业人员也纷纷选择Python语言来解决专业问题。 由于Python应用广泛,关于Python的参考书目前已经有很多,但将Python编程与数据分析、人工智
|
4天前
|
Python
python 代码脚本汇编
python 代码脚本汇编
15 0