Regex in Notepad++ "Replace Text + Value" ==> "New Text + Value"

Viewed 121

I rewrote some stuff and now I need to replace a lot of files they contain the following:

TOOL_ID = 1

and

TOOL_NAME = "Calc"

It needs to be replaced to:

Application_ID = (1)

and

Application_Name = "Calc"

My attempts are the following so far:

(?<=TOOL_ID = ) this returns at least the 1. But I am unsure how I can put it to a proper output. Since if I do:

Application_ID($&) it just replaces it, with : TOOL_ID = Application_ID1 (it don't even uses the "()")

Hope someone got some sweet tips :)

3 Answers

If you have those 2 values, you could use 2 capturing groups and use a conditional replacement.

\bTOOL_(ID|NAME)\h+=\h+(?:(\d+)|("Calc"))

Or to match word chars instead of Calc and ID or NAME

\bTOOL_([A-Z]+)\h+=\h+(?:(\d+)|("\w+"))

Explanation

  • \bTOOL_([A-Z]+) Match Tool_ and capture in group 1 1+ uppercase chars A-Z in group 1
  • \h+=\h+ Match = between 1+ horizontal whitespace chars
  • (?: Non capture group
    • (\d+) Capture group 2, match 1+ digits
    • | Or
    • ("\w+") Capture group 3, match 1+ word chars between double quotes
  • ) Close group

Regex demo

In the replacement test for group 2. If that exists, replace with group 1 and 2 or else replace with group 1 and 3.

Application_(?{2}$1 = \($2\):$1 = $3)

enter image description here

  • Ctrl+H
  • Find what: (?:(TOOL_ID) = (\d+)|(TOOL_NAME) =)
  • Replace with: (?1Application_id = \($2\))(?3Application_Name = )
  • CHECK Match case
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

(?:                     # non capture group
    (TOOL_ID)           # group 1, literally
    =                   # equal sign
    (\d+)               # group 2, value
  |                   # OR
    (TOOL_NAME) =       # group 3, literally
)

Replacement:

(?1                         # if group 1 exists (TOOL_ID)
    Application_id =          # replace with Application_id
    \($2\)                    # the value suround with parentheses that have to be escaped in Notepad++
)                           # end if
(?3                         # if group 3 exists (TOOL_NAME)
    Application_Name =        # replace with Application_Name
)                           # endif

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

This worked for me, apart from the ending parenthesis around the number 1: enter image description here

Related