2016-11-05 26 views
1

我想根据Python中的正常语法规则正确地分割一个语句。正则表达式使用Look Look或Look Look的正则表达式模式找到匹配的函数

我要拆分的一句话是

s = """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. 

对于很多搜索的我来到了以下的正则表达式这确实后实现这一目标,我使用定期, new_str是删除一些\ n从'''

m = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s',new_str) 

for i in m: 
    print (i) 



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 aprobability of .9 it isn't. 

所以我的方式了解了reg当然是我们首先从第一选择选择

1)所有像即

2字符)从过滤空间,我们选择那些不字符 有像夫人先生的话等等

3)从筛选的第2步我们只选择那些我们有点或问题,并在前面有一个空格的主题。

于是,我就改变顺序如下

1)先过滤掉所有的冠军。

2)从经滤波的步骤中选择那些由空间

3)之前除去所有的短语等即

但是当我做的是,坯件之后也分裂

m = re.split(r'(?<![A-Z][a-z]\.)(?<=\.|\?)\s(?<!\w\.\w.)',new_str) 

for i in m: 
    print (i) 


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 aprobability of .9 it isn't. 

修改过的程序中最后一步不应该能够识别短语,例如为什么它没有检测到它?

+0

您将使用nltk将文本拆分为句子,不可能在Python中编写精确的拆分正则表达式(您可以尝试一个匹配的正则表达式,但这将是一个挑战)。 –

+0

@WiktorStribiżew我同意,但在这种情况下,我想了解正则表达式的细微差别,以及为什么改变inorder会产生不正确的结果 –

+0

你想说'new_str'中的输入已经用像[这里](https://regex101.com/r/zEAkas/1)? –

回答

1

首先,最后.(?<!\w\.\w.)看起来很可疑,如果你需要匹配一个字面点,它逃脱它((?<!\w\.\w\.))。

说回正题,当你使用r'(?<![A-Z][a-z]\.)(?<=\.|\?)\s(?<!\w\.\w.)'正则表达式,最后负回顾后检查是否有空格后的位置不与字字符,圆点,文字字符,任何字符(之前因为.是转义)。这种情况是真的,因为有一个点,e,另一个.和该位置之前的空格。

要使回顾后的工作,同样的方式,当它是\s之前,把\s成回顾后发模式,太:​​

(?<![A-Z][a-z]\.)(?<=\.|\?)\s(?<!\w\.\w.\s) 

regex demo

另一个改进可以使用字符在第二个向后看:(?<=\.|\?) - >(?<=[.?])

+0

谢谢,所以当我使用lookback时,它会检查进行当前字符串的条件是否为真,因此不应该在此处开始查看空白,而不是查看空白之后的单词,因为前两个正则表达式选择了所有空格? –

+1

*不应该在这里开始查看空白* - 放置查找位置非常重要。如果将lookbehind放在'\ s'后面,则在*空格*之后搜索lookbehind模式。当lookbehind在'\ s'之前时,它在空白之前声明模式的存在(或不存在)。 *不适用于空白之后的单词* - 后置单词不会在这里以空白符后面查找一个单词,因为它紧挨着空白模式,因此只会在正好包含空格之前声明缺少模式。 –