2016-09-06 13 views
0

我正在Oracle中修復一些文本。問題是我的數據中的句子有句子沒有用空格分隔的詞。例如:?Oracle regexp_replace - 爲單獨的句子添加空格

  1. 句無句space.Between

  2. 一句問號第二句

我已經測試了以下在regex101 REPLACE語句,它似乎工作在那裏,但我不明白爲什麼它不能在Oracle中工作:

regexp_replace(review_text, '([^\s\.])([\.!\?]+)([^\s\.\d])', '\1\2 \3') 

這應該允許我查找分句時間段/感嘆號/問號(單個或分組),並在句子之間添加必要的空格。我意識到還有其他的方式可以將句子分開,但我上面的內容應該涵蓋大部分用例。第三個捕獲組中的\ d是爲了確保我不會意外更改諸如「4.5」到「4. 5」之類的數值。

測試組之前:

Sentence without space.Between sentences 
Sentence with space. Between sentences 
Sentence with multiple periods...Between sentences 
False positive sentence with 4.5 Liters 
Sentence with!Exclamation point 
Sentence with!Question mark 

後的變化應該是這樣的:

Sentence without space. Between sentences 
Sentence with space. Between sentences 
Sentence with multiple periods... Between sentences 
False positive sentence with 4.5 Liters 
Sentence with! Exclamation point 
Sentence with! Question mark 

Regex101鏈接:https://regex101.com/r/dC9zT8/1

雖然所有變化工作從regex101預期,我的問題是,我進入Oracle的原因是我的第三個和第四個測試用例沒有按預期工作。 Oracle不會在多個句點(省略號)組之後添加空格,而regexp_replace會爲「4.5」添加空格。我不確定爲什麼會出現這種情況,但也許有一些關於我缺少的Oracle regexp_replace的特性。

任何和所有的見解是值得讚賞的。謝謝!

+0

我的猜測是它是在regex101中打開的全局匹配(g標誌),而不是在Oracle中打開。 –

+0

全局發生是我沒有想到的,但即使在Oracle中使用setting = 0時,我仍然遇到同樣的問題。 – flamewheel

回答

2

這可能會讓你開始。這將檢查。?!以任意組合,接着是零個或多個空格和一個大寫字母,並且它將用一個空格替換「零個或多個空格」。這不會分隔十進制數字;但它會錯過以大寫字母以外的任何字符開頭的句子。您可以開始添加條件 - 如果遇到困難,請回復,我們會盡力提供幫助。參考其他正則表達式可能會有幫助,但它可能不是獲得答案的最快方法。

with 
    inputs (str) as (
     select 'Sentence without space.Between sentences'   from dual union all 
     select 'Sentence with space. Between sentences'    from dual union all 
     select 'Sentence with multiple periods...Between sentences' from dual union all 
     select 'False positive sentence with 4.5 Liters'   from dual union all 
     select 'Sentence with!Exclamation point'     from dual union all 
     select 'Sentence with!Question mark'      from dual 
    ) 
select regexp_replace(str, '([.!?]+)\s*([A-Z])', '\1 \2') as new_str 
from inputs; 

NEW_STR 
------------------------------------------------------- 
Sentence without space. Between sentences 
Sentence with space. Between sentences 
Sentence with multiple periods... Between sentences 
False positive sentence with 4.5 Liters 
Sentence with! Exclamation point 
Sentence with! Question mark 

6 rows selected. 
+0

謝謝mathguy - 你寫的是合乎邏輯的聲音。我將應用你給出的內容(儘管我也會使用小寫字母a-z)並檢查是否缺少任何東西。 – flamewheel