2017-02-24 119 views
0

我有一個解析CSV文件的程序。不幸的是,如果分隔符在括號內,程序無法處理。現在我想找到一個正則表達式,如果它在括號內,就會找到分隔符。正則表達式 - 在括號內僅匹配分隔符

Name;Zip;Comment 
Smith,12345;"Weird comment with ; inside" 

以下正則表達式返回整個註釋字段,但我只想要;括號

("[^;]*;[^;]*") 

回答

0

內角色你可以試試這樣

r\".*(;).*"\ 

在這種情況下,一些正則表達式,正則表達式將匹配整個註釋。但是,捕獲組1(括號內的部分)將與雙引號內的;匹配。

+0

不幸的是我沒有可能指定應該使用哪個捕獲組。正則表達式的整個匹配將被替換爲我選擇的一個字符。 – user1126181

0

您應使用以下正則表達式使用捕捉組捕捉整個評論並更換分號:

(".*?)(;\s)(.*?") 

input  >> Name;Zip;Comment 
       Smith,12345;"Weird comment with ; inside" 
match  >> (".*?)(;\s)(.*?") 
replace with >> $1$3 
output  >> Name;Zip;Comment 
       Smith,12345;"Weird comment with inside" 

看到demo/explanation

相關問題