Search code examples
regexreplaceeditpad

EditPad: How to replace multiple search criteria with multiple values?


I did some searching and found tons of questions about multiple replacements with Regex, but I'm working in EditPadPro and so need a solution that works with the regex syntax of that environment. Hoping someone has some pointers as I haven't been able to work out the solution on my own.

Additional disclaimer: I suck with regex. I mean really... it's bad. Like I barely know wtf I'm doing.So that being said, here is what I need to do and how I'm currently approaching it...

I need to replace two possible values, with their corresponding replacements. My two searches are:

(.*)-sm (.*)-rad

Currently I run these separately and replace each with simple strings:

sm rad

Basically I need to lop off anything that comes prior to "sm" so I just detect everything up to and including sm, and then replace it all with that string (and likewise for "rad").

But it seems like there should be a way to do this in a single search/replace operation. I can do the search part fine with:

(.*)-sm|(.*)-rad

But then how to replace each with it's matching value? That's where I'm stuck. I tried:

sm|rad

but alas, that just becomes the literal complete string that is used for replacement.


Solution

  • Search for:

    (.*)-(sm|rad)
    

    Now, when you put something in parenthesis in Regex, those matches are stored in temporary variables. So whatever matched (.*) is stored in \1 and whatever matched (sm|rad) is stored in \2. Therefore, you want to replace with:

    \2
    

    Note that the replacement variable may be different depending on what programming language you are using. In Perl, for example, I would have to use $2 instead.