导航:首页 > 编程语言 > python正则表达式多行匹配

python正则表达式多行匹配

发布时间:2022-07-19 18:42:11

1. 正则表达式如何满足多行和跨行匹配

正则:

且加上多行的参数是:

python

完整代码:

(网络知道的编辑器中竟然不能输入代码,鄙视👎之)

"""

Function;

正则表达式如何满足多行和跨行匹配?_网络知道

https://..com/question/989230535019059459.html

Author: Crifan Li

Update: 20200208

"""

import re

yourMultipleLineStr = """

first line dog

second line cat

third line no animal

"""

foundMatched = re.search("dog.+cat", yourMultipleLineStr, re.S)

print("foundMatched=%s" % foundMatched)

if foundMatched:

matchedStr = foundMatched.group(0)

print("matchedStr=%s" % matchedStr)

# foundMatched=<re.Match object; span=(12, 31), match='dog second line cat'>

# matchedStr=dog

# second line cat



具体语法详见:

re --- 正则表达式操作 — Python 3.8.1 文档

  • re.S

  • re.DOTALL

  • 让'.'特殊字符匹配任何字符,包括换行符;如果没有这个标记,'.'就匹配除了换行符的其他任意字符。对应内联标记(?s)。

-》就可以让上面的 点. 可以匹配到 换行符 -》 就可以去跨行去匹配了


在线测试效果截图:

RegExr: Learn, Build, & Test RegEx


更多内容,详见我的教程:

应用广泛的超强搜索:正则表达式

2. python3 正则表达式如何匹配多段内容,举例如下:(中间需要通配掉许多字符)

\ 应该转义吧?试试这样写:

pattern = re.compile(r'href=(.*?) target="_blank" title=(.*?)>.*?timestyle4222">(.*?)\xa0')



pattern = re.compile('href=(.*?) target="_blank" title=(.*?)>.*?timestyle4222">(.*?)\\xa0')

3. Python正则表达式的几种匹配方法

1.测试正则表达式是否匹配字符串的全部或部分
regex=ur"" #正则表达式
if re.search(regex, subject):
do_something()
else:
do_anotherthing()

2.测试正则表达式是否匹配整个字符串

regex=ur"/Z" #正则表达式末尾以/Z结束
if re.match(regex, subject):
do_something()
else:
do_anotherthing()

3.创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches (part of) a string)

regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
# match start: match.start()
# match end (exclusive): atch.end()
# matched text: match.group()
do_something()
else:
do_anotherthing()

4.获取正则表达式所匹配的子串(Get the part of a string matched by the regex)

regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
result = match.group()
else:
result = ""

4. python 正则表达式 匹配多行聊天记录的问题。

如果你所指得[任意文字]的意思是1个或1个以上文字得话
你可以试试下面得语句
p = re.compile(u'.*想知道.+\n.+也想知道.+')

否则你只需要将里面得"+"改为"*"就可以匹配0得情况
也就是下面得语句
p = re.compile(u'.*想知道.*\n.*也想知道.*')

测试代码如下:
>>>a = u"""我想知道。
..... 我也想知道。"""
>>>p.search(a)
>>><_sre.SRE_Match object at 0x1014d5100>

看了你的问题补充,如果你想要查找出字符串中最后匹配的子字符串的话,我目前没想到比较好的办法。
如果一定要用正则的话你可以考虑采用遍历的方式,也就是匹配所有不含abc的情况。
比如说^a, ^b, ^c, ab^c, a^bc....等等等等。不过这样以来正则表达式会显得过于复杂,而且扩展行几乎为0。
如果可以不用正则,那python本身的库就能够实现(可能你嫌处理语句过多或考虑到时间问题而不想用),而且逻辑也较为简单(我觉得是这样)。python的string类中自带有find()和rfind()方法再加上split()方法,合理使用的话应该能找出所有的匹配子字符串。

5. Python正则表达式的几种匹配用法

下面列出: 1.测试正则表达式是否匹配字符串的全部或部分regex=ur"" #正则表达式
if re.search(regex, subject): do_something()else: do_anotherthing() 2.测试正则表达式是否匹配整个字符串 regex=ur"/Z" #正则表达式末尾以/Z结束
if re.match(regex, subject): do_something()else: do_anotherthing() 3.创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches (part of) a string) regex=ur"" #正则表达式
match = re.search(regex, subject)if match: # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() do_something()else: do_anotherthing() 4.获取正则表达式所匹配的子串(Get the part of a string matched by the regex) regex=ur"" #正则表达式
match = re.search(regex, subject)if match: result = match.group()else: result ="" 5. 获取捕获组所匹配的子串(Get the part of a string matched by a capturing group) regex=ur"" #正则表达式
match = re.search(regex, subject)if match: result = match.group(1)else: result ="" 6. 获取有名组所匹配的子串(Get the part of a string matched by a named group) regex=ur"" #正则表达式
match = re.search(regex, subject)if match:result = match.group"groupname")else:result = "" 7. 将字符串中所有匹配的子串放入数组中(Get an array of all regex matches in a string) result = re.findall(regex, subject) 8.遍历所有匹配的子串(Iterate over all matches in a string) for match in re.finditer(r"<(.*?)/s*.*?//1>", subject) # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() 9.通过正则表达式字符串创建一个正则表达式对象(Create an object to use the same regex for many operations) reobj = re.compile(regex) 10.用法1的正则表达式对象版本(use regex object for if/else branch whether (part of) a string can be matched) reobj = re.compile(regex)if reobj.search(subject): do_something()else: do_anotherthing() 11.用法2的正则表达式对象版本(use regex object for if/else branch whether a string can be matched entirely) reobj = re.compile(r"/Z") #正则表达式末尾以/Z 结束
if reobj.match(subject): do_something()else: do_anotherthing() 12.创建一个正则表达式对象,然后通过该对象获得匹配细节(Create an object with details about how the regex object matches (part of) a string) reobj = re.compile(regex) match = reobj.search(subject)if match: # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() do_something()else: do_anotherthing() 13.用正则表达式对象获取匹配子串(Use regex object to get the part of a string matched by the regex) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group()else: result ="" 14.用正则表达式对象获取捕获组所匹配的子串(Use regex object to get the part of a string matched by a capturing group) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group(1)else: result ="" 15.用正则表达式对象获取有名组所匹配的子串(Use regex object to get the part of a string matched by a named group) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group("groupname")else: result ="" 16.用正则表达式对象获取所有匹配子串并放入数组(Use regex object to get an array of all regex matches in a string) reobj = re.compile(regex) result = reobj.findall(subject) 17.通过正则表达式对象遍历所有匹配子串(Use regex object to iterate over all matches in a string) reobj = re.compile(regex)for match in reobj.finditer(subject): # match start: match.start() # match end (exclusive): match.end() # matched text: match.group()字符串替换 1.替换所有匹配的子串 #用newstring替换subject中所有与正则表达式regex匹配的子串
result = re.sub(regex, newstring, subject) 2.替换所有匹配的子串(使用正则表达式对象) reobj = re.compile(regex) result = reobj.sub(newstring, subject) 字符串拆分 1.字符串拆分 result = re.split(regex, subject) 2.字符串拆分(使用正则表示式对象) reobj = re.compile(regex) result = reobj.split(subject)

6. python re正则表达式多匹配头单匹配尾如何最小匹配

importre

string='''<imgsrc="https://img3.doubanio.com/f/shire//pics/blank.gif"data-origin="https://img1.doubanio.com/view/photo/albumcover/public/p2519116699.jpg"alt=""/>'''

regex=re.findall(r'data-origin="([^"]+.jpg)"',string)
print(regex[0])

7. python正则表达式匹配满足条件的两行

所以,你这是在多行文件中找出任意2行符合条件的,还是2行就算一组数据,里面有多组数据?

r=re.compile(r'[S]+|([AGCT]{12})([AGCT]{12})[s]+163[s]+[w]+[s]+([d]+)(?:[s]+[S]+){3}[s]+([d]+)(?:[s]+[S]+){2}[
]+'
r'[S]+|21[s]+993[s]+[w]+[s]+3(?:[s]+[S]+){3}[s]+4(?:[s]+[S]+){2} ')

。。。

8. python正则表达式提取多个匹配内容

替换掉不就行咯,像这样:
re.sub(r'<[A-Z]+>',' ',‘<SPAN><P>eng li aas<SS>ddde<AP>iiiiideeeeef<P>
’)
或者:
>>> ' '.join(re.split(r'<[A-Z]+>','<SPAN><P>eng li aas<SS>ddde<AP>iiiiideeeeef<P>'))
' eng li aas ddde iiiiideeeeef '
>>>

阅读全文

与python正则表达式多行匹配相关的资料

热点内容
python在excel模板生成数据 浏览:47
数位分离并求和python 浏览:39
河池源码出售最新行情 浏览:741
晓龙服务器怎么样 浏览:321
androidwidget图片 浏览:833
95压缩比与汽油标号 浏览:752
算法岗位需要学什么专业研究生 浏览:669
银行卡忘了怎么登录手机app 浏览:962
加密双菠萝帽流苏挂件 浏览:886
云服务器后台编程技巧 浏览:997
python人工智能搭建 浏览:250
安卓m6用什么下载 浏览:1000
对程序员有偏见吗 浏览:292
如何让服务器运行缓慢 浏览:238
黑马程序员入学流程 浏览:448
win732位安装python什么版本 浏览:786
压缩方式标准 浏览:558
免费低吸指标源码 浏览:184
MO命令是 浏览:47
python入门常见错误 浏览:411