2012-10-03 114 views
1

我正在維護一箇舊系統,當一個字符串包含單引號時可以節省問題。正則表達式,找到單引號括起來的單引號

例如,這些將失敗:

"update table set column2 = 'O'Connell', column3 = 'O'Riordon' where column1 = 1" 
"insert into table (column1, column2, column3, column4, column5, column6, column7) values('O'Reilley','state, postcode','',1,2,'O'Riordon')". 

到目前爲止,我想出了這個工作的VBScript正則表達式

([,=(]\s*'[^']*)'([^']*'\s*[,)\s]) 

是否可以寫一個VBScript正則表達式,而不使用頭[(| = | \ s |,]和預告片[,|)| \ s]?

謝謝。

編輯:修復發佈的正則表達式刪除|從頭部和拖車。 的正則表達式的使用步驟如下

regexp.replace("string","$1''$2") 
+0

除了'[=(]'和'[ ,)\ s]',而且爲什麼後者包含'\ s'這令人困惑:你爲什麼要刪除它們?如果他們走了,你怎麼知道比賽開始或結束的地方? –

+0

@TimPietzcker感謝您的糾正,結尾\是匹配更新陳述「Column3 ='O'Riordon'」。 – Jules

+0

沒錯,所以如果你刪除了正則表達式的那部分,你還知道比賽開始或結束的位置?你顯然得到了不正確的數據,所以如果有人能夠說出需要遵循哪些規則來挽救這個問題,那就是你。 –

回答

1

我能想到的作品僅包含單引號從未字符串中包含空格的唯一途徑。在這種情況下,您可以搜索

\B'([^'\s]*'[^'\s]*)'\B 

說明:

\B  # Assert that there is no alphanumeric character before... 
'   # the opening quote 
(  # Match and capture... 
[^'\s]* # Any number of non-quote/non-whitespace characters 
'  # One quote 
[^'\s]* # Any number of non-quote/non-whitespace characters 
)   # End of capture group 1 
'   # Match the closing quote 
\B  # Assert that there is no alphanumeric character afterwards 
一個事實,即這些字符類的正確語法本來
+0

Thx爲您提供幫助。 – Jules

相關問題