2011-12-24 380 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

相关问题