Search code examples
replacenotepad++

Notepad++ / RegEx - Find & replace multiple parts of a line while ignoring any words between


I appreciate what I'm asking may be very simple to more experienced folks. I've spent several hours trying to get my head around RegEx and have gotten close to what I need, but as this is something I'm trying to achieve for a hobby project (RegEx is not something I require in my day job) I'm hoping some of you may be able to help me out.

In short, I have a very large file with tens of thousands of lines of code that I am converting to be readable by another program. All I need to accomplish this is to change some formatting.

I need to find every instance where the tag "{@graphic examplename}" is used, and change it so that only "examplename" remains in square [[ ]] brackets.

Examples of how the tags currently appear (example names can be either single words or multiple):

"{@graphic example1}",
"{@graphic example2}",
"{@graphic example3 with multiple words}"

What I want them to look like when done, replacing the { with [[, removing @graphic, and replacing } with ]].

"[[example1]]",
"[[example2]]",
"[[example3 with multiple words]]"

It's easy enough to do a simple find-and-replace to replace "{@graphic " with "[[", as the @graphic tag is something I want to remove universally however the issue I'm running into is that I can't replicate that with the "}" at the end, because I can't find a way to specify that I only want to replace examples of "}" that come after an instance of "{@graphic " while leaving any other words (the examplename) intact.

Any assistance gratefully received - if the above needs any elaboration please don't hesitate to ask, I understand I may be putting this in amateurish terms.

Regards, K


Solution

  • Often programs have a way of capturing groups and referencing them later, often with $

    so find {@graphic ([^}]+)}

    replace [[$1]]

    Captures what is inside the () and makes it available in the replace as $1, i.e. the first "capture group".

    Regex 101 is an excellent resource for trying these things out:

    https://regex101.com/r/r5OX8I/1