Pergunta

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.

Foi útil?

Solução 2

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.

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top