2017-02-23 36 views
1

我有諸如數據:如何使用正則表達式提取否定句

實施例1

Barium swallow : There is slow transit to the stomach. 
There is lots of other stuff going on. 
There is no aspiration. 
Conclusion: Appearances are consistent with achalasia. 

實施例2

Barium swallow : There is slow transit to the stomach. 
There is no evidence of achalasia here. 
There is no aspiration. 
Conclusion: Normal study 

實施例3

Barium swallow : There is slow transit to the stomach. 
There is no reflux. 
There is no aspiration. 
Conclusion: Possible achalasia 

我想擺脫之間的詞但不限於一個行,以便與上面的例子中我得到

實施例1

Barium swallow : There is slow transit to the stomach. 
There is lots of other stuff going on. 
There is no aspiration. 
Conclusion: Appearances are consistent with achalasia. 

實施例2

Barium swallow : There is slow transit to the stomach. 
There is. 
There is no aspiration. 
Conclusion: Normal study 

實施例3

Barium swallow : There is slow transit to the stomach. 
There is no reflux. 
There is no aspiration. 
Conclusion: Possible achalasia 

要做到這一點(在R)我使用以下正則表達式:

[Nn]o.*[Aa]chalasia.*?(\\.\\n|\\.$) 

然而與此我得到:

實施例1對

Barium swallow : There is slow transit to the stomach. 
There is lots of other stuff going on. 
There is no 

實施例2

Barium swallow : There is slow transit to the stomach. 
There is no aspiration. 
Conclusion: Normal study 

實施例3

Barium swallow : There is slow transit to the stomach. 
There is no reflux. 
There is no 

回答

1

嘗試此來代替。

/(?i)\s?no\s.*achalasia/

它不應該跨行匹配。

在Ruby:

string.gsub!(/(?i)\s?no\s.*achalasia/, '') 
+0

所以,真正的一部分我感興趣的是在這之間的變化(即有......)之間的否和失弛緩症。在這裏,換新界線的比賽會停止嗎? –

0

您可以使用下面的正則表達式

\bno(.*?)achalasia\b 

紅寶石

re = /\bno(.*?)achalasia\b/ 
str = 'Barium swallow : There is slow transit to the stomach. 
There is lots of other stuff going on. 
There is no aspiration. 
Conclusion: Appearances are consistent with achalasia. 
Barium swallow : There is slow transit to the stomach. 
There is no evidence of achalasia here. 
There is no aspiration. 
Conclusion: Normal study 
Barium swallow : There is slow transit to the stomach. 
There is no reflux. 
There is no aspiration. 
Conclusion: Possible achalasia 
There is no evidence of achalasia here.' 

# Print the match result 
str.match(re) do |match| 
    puts match.to_s 
end 
0

行,所以我想通了這一點。這個問題是不是正則表達式,而是使用GSUB有R爲你使用選項Perl的正則表達式時,事實= T

然後我用[Nn]o.*[Aa]chalasia.*?(\\.|$)

其中的伎倆