regular expression to add characters before and after numbers
Asked Answered
A

3

13

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.

Arithmomancy answered 24/12, 2011 at 10:43 Comment(1)
While the regex sill stay the same, the code will change: WHich Language?Britneybritni
F
25

Find What: (\[\d+])

Replace With: xxxxxxxxx \1 xxxxxxxxx

enter image description here

February answered 24/12, 2011 at 10:49 Comment(4)
@Arithmomancy - You can accept as answer by clicking on the tick on the left.February
No need for parentheses if you're going to use the entire match anyway - replace with xxxxxx \0 xxxxxx.Frosting
Really? Interesting but not surprising given Notepad++'s lackluster regex support.Frosting
don't forget to put your search statement into parenthesisIllailladvised
A
5

Regular expression:

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


Refer: regexr

Abbreviate answered 5/7, 2017 at 10:5 Comment(0)
B
2

C#:

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

Other languages analogous

Britneybritni answered 24/12, 2011 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.