Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python正则表达式,条件搜索_Python_Regex - Fatal编程技术网

Python正则表达式,条件搜索

Python正则表达式,条件搜索,python,regex,Python,Regex,我想把这句话分开 ['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... "] “史密斯先生花150万美元买下了cheapsite.com,也就是说,他花了很多钱”\ “为了它。他介意吗?小亚当·琼斯

我想把这句话分开

['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... "]
“史密斯先生花150万美元买下了cheapsite.com,也就是说,他花了很多钱”\
“为了它。他介意吗?小亚当·琼斯认为他不介意。无论如何,这个”\
“不是真的……好吧,概率为.9,不是。”
进入下面的列表

['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... "]
史密斯先生花150万美元买下了cheapsite.com,也就是说,他为此付出了很多。
他介意吗?
小亚当·琼斯认为他没有。
无论如何,这不是真的。。。
嗯,概率是9,不是。
代码:

['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... "]
输出:

['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... "]
['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."] 
史密斯先生花了150万美元买下了cheapsite.com,也就是说他付钱了 “很多钱。”,“小亚当·琼斯认为他没有。” K gud,但它遗漏了一些,有没有办法告诉Python,因为上次[^a-z]不是我小组的一部分,请继续从那里搜索

['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... "]
编辑:

['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... "]
这是通过@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... "]
输出:

['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... "]
但我们仍然需要最后一句话。有什么想法吗?

试试这个:

['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)
使用正向前瞻正则表达式技术,最后检查

['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)
以上工作完美的需要。包括最后一句话。但是我不知道为什么
|。*.$
有效,请帮助我理解

['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... "]
输出:

['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... "]
['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."] 
(.+?)(?
试试这个。参见演示。抓取捕获或组。使用
re.findall

['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... "]

哇,正则表达式很棒,非常好用。Thx@sputnick。什么是
?=
真正的意思?这是积极前瞻的语法,请查看我的answernice教程中添加的链接链接,是否有办法也包括最后一句话,即“排除在点后寻找空格和[^a-z]它是文件的结尾。类似于word Boundaries的内容结尾没有空格:
re.findall('[A-Z]+[^.].*?[A-Z.][.?!](?:(?=[^A-Z])|$),text)
related:。
['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... "]