regular expression to add characters before and after numbers

43,599

Solution 1

Find What: (\[\d+])

Replace With: xxxxxxxxx \1 xxxxxxxxx

enter image description here

Solution 2

Regular expression:

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


Refer: regexr

Solution 3

C#:

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

Other languages analogous

Share:
43,599
Mike
Author by

Mike

Updated on July 06, 2020

Comments

  • Mike
    Mike almost 4 years

    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.

  • manojlds
    manojlds over 12 years
    @Mike - You can accept as answer by clicking on the tick on the left.
  • Tim Pietzcker
    Tim Pietzcker over 12 years
    No need for parentheses if you're going to use the entire match anyway - replace with xxxxxx \0 xxxxxx.
  • Tim Pietzcker
    Tim Pietzcker over 12 years
    Really? Interesting but not surprising given Notepad++'s lackluster regex support.
  • JinSnow
    JinSnow over 7 years
    don't forget to put your search statement into parenthesis