regular expression to add characters before and after numbers

我怕爱的太早我们不能终老 提交于 2019-12-22 03:48:25

问题


I have a list of numbers between square brackets, and I need to add words before and after the exact numbers (i.e. keep the same numbers). I use notepad++ to replace, but if you have a solution with other program please advise.

Example:

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

outcome:

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

The numbers are of course \d+ but I want to tell it to keep the same numbers when looking.


回答1:


Find What: (\[\d+])

Replace With: xxxxxxxxx \1 xxxxxxxxx




回答2:


C#:

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

Other languages analogous




回答3:


Regular expression:

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


Refer: regexr



来源:https://stackoverflow.com/questions/8624018/regular-expression-to-add-characters-before-and-after-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!