在Regex中查找“|”之间的句子

2024-06-01 03:18:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我在从网站上搜集的数据中寻找介于这两者之间的任何东西。 我注意到,“|”分隔了所有我感兴趣的东西。你知道吗

["{somethingsomething|title=hello there!\n|subtitle=how are you\n|subsubtitle=I'm good, thanks\n}"]

我想打印:

title=hello there!
subtitle=how are you
subsubtitle= I'm good, thanks

我想我应该使用look behind和look ahead,比如this,但是当它位于“|”字符之间时,它就不起作用了。你知道吗

我猜是这样的:

(?<=title=)(.*)(?=subtitle=)

(我对RegEx非常陌生,但渴望学习!)你知道吗


Tags: 数据youhellotitle网站are感兴趣how
3条回答

正则表达式仅在处理复杂字符串时才是必需的。像这样的简单字符串只能使用字符串函数处理:

a = "[\"{somethingsomething|title=hello there!\n|subtitle=how are you\n|subsubtitle=I'm good, thanks\n}\"]"
b = a.lstrip('["{')
c = b.rstrip('}"]')
c.split('|')
# ['somethingsomething',
# 'title=hello there!\n',
# 'subtitle=how are you\n',
# "subsubtitle=I'm good, thanks\n"]

如果您真的必须为此使用正则表达式,请不要用不必要的lookback和lookahead使它们过于复杂。这些位是您试图匹配的模式的一部分,只需这样使用它们:

title=(.*?)[|]subtitle=(.*?)[|]subsubtitle=(.*?)}

Regular expression visualization

Debuggex Demo

注意,我还在前缀中包含了|,因为否则|字符将作为每个组的一部分结束。我把你们每个贪婪的.*组变成了一个非贪婪的.*?。如果要匹配所有的组,这实际上是没有必要的,但是在您的原始示例中,这就是标题最终包含到sub为止的所有内容,并且子标题最终作为副标题的原因。最后,我把}放在末尾,这样就不会把整个外部分组作为子标题的一部分。你知道吗

可以使用split()方法:

In [5]: data = "{somethingsomething|title=hello there!\n|subtitle=how are you\n|subsubtitle=I'm good, thanks\n}"[1:-1]
In [6]: data
Out[6]: "somethingsomething|title=hello there!\n|subtitle=how are you\n|subsubtitle=I'm good, thanks\n"
In [7]: data = data.replace("\n", "")
In [8]: data
Out[8]: "somethingsomething|title=hello there!|subtitle=how are you|subsubtitle=I'm good, thanks"
In [9]: words = data.split("|")
In [10]: words
Out[10]: 
['somethingsomething',
 'title=hello there!',
 'subtitle=how are you',
 "subsubtitle=I'm good, thanks"]
In [11]: title = words[1].split("=")[1]
In [12]: title
Out[12]: 'hello there!'
In [13]: suttitle =  words[2].split("=")[1]
In [14]: suttitle
Out[14]: 'how are you'
In [15]: subsuttitle = words[3].split("=")[1]
In [16]: subsuttitle
Out[16]: "I'm good, thanks"

相关问题 更多 >