0

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.

JVC
  • 793
  • 3
  • 8
  • 21

2 Answers2

2

Jonathan, first off let me congratulate you for using EPP Pro for regex in your text. It's my main text editor, and the main reason I chose it, as a regex lover, is that its support of regex syntax is vastly superior to competing editors. For instance Notepad++ is known for its shoddy support of regular expressions. The reason of course is that EPP's author Jan Goyvaerts is the author of the legendary RegexBuddy.

A picture is worth a thousand words... So here is how I would do your replacement. Just hit the "replace all button". The expression in the regex box assumes that anything before the dash that is not a whitespace character can be stripped, so if this is not what you want, we need to tune it.

EPP regex screenshot

zx81
  • 41,100
  • 9
  • 89
  • 105
  • This solution works perfectly and since it was given first, I will award it as the correct answer. Thanks so much! – JVC Apr 22 '14 at 22:54
  • I apologize, I misread the times of the answers given, this one was actually given second so I am awarding correct answer to the first response. Sorry again for any confusion! – JVC Apr 22 '14 at 23:02
1

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.

mareoraft
  • 3,474
  • 4
  • 26
  • 62
  • Ignore my previous comment, which I am now editing. Thanks for this, it works great! – JVC Apr 22 '14 at 22:50
  • I apologize, I misread the times of the answers given, this one was actually given first so I am awarding it correct answer. Sorry again for any confusion! – JVC Apr 22 '14 at 23:03