2011-12-30 128 views
1

我需要在字符串中找到一個數字值+引號(示例5「)。一旦找到,我需要用」磅「替換引號。串查找正則表達式並替換特定字符

Work and run with Joe "The King" Mel using a 3" vest with "Dog-Bone" weights. 

我已經試過

SELECT REPLACE(REPLACE('Work and run with Joe "The King" Mel using a 3" vest with "Dog-Bone" weights.', '%[^0-9]"%', 'pound'), '"', 'pound') 

但是它與「英鎊」替換所有的報價。

回答

4

這將找到一個數字,後跟一個"的第一次出現,並與pound取代"

declare @s varchar(100) 
set @s = 'Work and run with Joe "The King" Mel using a 3" vest with "Dog-Bone" weights.' 
select stuff(@s, patindex('%[0-9]"%', @s)+1, 1, ' pound') 

STUFF

PATINDEX

如果你有多個,你可以把它放在一個while循環。

while patindex('%[0-9]"%', @s) > 0 
begin 
    set @s = stuff(@s, patindex('%[0-9]"%', @s)+1, 1, ' pound') 
end 
+0

的偉大工程。 ..用PATINDEX默認值0修正了一些錯誤。 – 2011-12-30 19:02:43

0

如果你不能表達你想要的變化確定性算法做出來,你永遠無法寫出一個正則表達式作爲一個實現。

你已經寫了兩個替換表達式:一個在數字後面只替換引號,但另一個替換所有引號,句點。

+0

我試圖代碼,以查找包含在正則表達式「%[^ 0-9]「%引號的位置,然後做該特定字符位置的更換,但沒有工作.. – 2011-12-30 18:04:00