2011-12-24 375 views
5

我有一個方括號內的數字列表,我需要在確切數字前後添加單詞(即保留相同的數字)。我用記事本++來替換,但如果你有其他程序的解決方案,請告知。在數字前後添加字符的正則表達式

例子:

text [121] othertext 
moretext [16] othertextmore 
andtext [5940] othertextplus 

結果:

text xxxxxxxxx [121] xxxxxxxxx othertext 
moretext xxxxxxxxx [16] xxxxxxxxx othertextmore 
andtext xxxxxxxxx [5940] xxxxxxxxx othertextplus 

的數字當然\d+,但我要告訴它繼續尋找時,相同的數字。

+0

雖然正則表達式門檻保持不變,代碼將變爲:哪種語言? – 2011-12-24 10:48:35

回答

16

查找內容:(\[\d+])

替換爲:xxxxxxxxx \1 xxxxxxxxx

enter image description here

+0

完美..謝謝:) – Mike 2011-12-24 10:53:19

+1

@Mike - 您可以通過點擊左側的勾號接受答案。 – manojlds 2011-12-24 10:53:58

+0

如果您打算使用整個匹配,則不需要括號 - 用「xxxxxx \ 0 xxxxxx」替換。 – 2011-12-24 10:56:43

2

C#:

line=Regex.Replace(line,@"([^\[])(\[\d+\])(.*)","$1xxxxxxxxx $2 xxxxxxxxx$3"); 

類似於其他語言

0

正則表達式:

Find regex = \[\d+\] 
Replace regex = xxxxxxxxx$&xxxxxxxxx 


參見:regexr

相關問題