Search code examples
regexnotepad++

Regexp: How to find and replace brackets around numbers in notepad++


I would like to do a regex replace operation in notepad++

here is the test string:

td{2}(i,j),
td{3}(i,j),
td{4}(i,j),

And I would like to end up with this:

td(2,i,j),
td(3,i,j),
td(4,i,j),

I can find the string, but I don't know how to write the replacement string.

\{\d{1}\}\(

I tried the above expression with (.)$ to find the number

\{\d{1}\}\(

and this for the replacement string but it didn't work

 \($1,

How can I get the results I want?


Solution

  • You can use

    \{(\d)}(\()
    

    Replace with $2$1,. Also, see the regex demo. Details:

    • \{ - a { char
    • (\d) (or (\d+) to match more than one digit) - Group 1 ($1): a digit
    • } - a } char
    • (\() - Group 2 ($2): a ( char.

    The replacement is a concatenation of Group 2 (the ( char) + Group 1 (the digit(s)) + ,. If you do not capture ( into Group 2 (i.e. if you use \{(\d)}\( or \{(\d+)}\(), you could use the \($1, replacement, where ( needs escaping.

    See the demo screenshot with settings:

    enter image description here