gpt4 book ai didi

Python 正则表达式,条件搜索

转载 作者:太空宇宙 更新时间:2023-11-04 01:11:02 24 4
gpt4 key购买 nike

我正在尝试拆分这句话

"Mr. Smith bought cheapsite.com for 1.5 million dollars, i.e. he paid a lot " \
"for it. Did he mind? Adam Jones Jr. thinks he didn't. In any case, this " \
"isn't true... Well, with a probability of .9 it isn't."

进入下面的列表。

Mr. Smith bought cheapsite.com for 1.5 million dollars, i.e. he paid a lot for it.
Did he mind?
Adam Jones Jr. thinks he didn't.
In any case, this isn't true...
Well, with a probability of .9 it isn't.

代码:

print re.findall('([A-Z]+[^.].*?[a-z.][.?!] )[^a-z]',text)

输出:

['Mr. Smith bought cheapsite.com for 1.5 million dollars, i.e. he paid
a lot for it. ', "Adam Jones Jr. thinks he didn't. "]

K gud,但它遗漏了一些,有没有办法告诉 Python,因为最后一个 [^a-z] 不属于我的组,请从那里继续搜索。

编辑:

这是通过@sputnick 提到的前瞻性正则表达式实现的。

print re.findall('([A-Z]+[^.].*?[a-z.][.?!] )(?=[^a-z])',text)

输出:

['Mr. Smith bought cheapsite.com for 1.5 million dollars, i.e. he paid
a lot for it. ', 'Did he mind? ', "Adam Jones Jr. thinks he didn't. "
, "In any case, this isn't true... "]

但是我们还需要最后一句。有什么想法吗?

最佳答案

试试这个:

print re.findall('([A-Z]+[^.].*?[a-z.][.?!] )(?=[^a-z])',text)

使用正向预测正则表达式技术,检查http://www.regular-expressions.info/lookaround.html

关于Python 正则表达式,条件搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27650853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com