reg exp help

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
Erik

reg exp help

Post by Erik »

fairly new at regular expression replacing, but a quick question that I couldn't figure out. How would I go about matching the following;

%EJ%erik <-- this "erik" is NOT matched because it has "%EJ%" in front of it

erik was here <-- this "erik" is matched
derik <--- this "erik" is matched even though it's within a word

I guess this is an exception to a matching rule.... match all occurences of "erik" except those that have "%EJ%" immediately in front of it.

I've tried using the "^" operator but it works on characters not a specific set of characters.

Thanks for any help!
Andreas

Re: reg exp help

Post by Andreas »

Try this:

Find
\([^%]\)erik

replace
\1john

explanation:
\(\) everything in between these is remembered, the first such pair can be used with \1, the second with \2 and so on till \9

[] finds any character that is given within the []
[^] finds any character that is NOT within the [^]
[^%] finds any non-percent-sign

\([^%]\)erik finds a non-percent-sign (whatever the character is, it will be remembered because of the \(\) around it) followed by erik

it is replaced by whatever character was found for the first \(\) - in this case the character preceding erik if it is not a percent sign followed by john

so
%EJ%erik is not changed
derik is changed to djohn
something erik was here is changed to something john was here
erik at the beginning of a line is not changed, so you need a second go with

find
^erik
replace
john
Erik

Re: reg exp help

Post by Erik »

Ok I think I follow everything you got going there but what will happen with the string "%%erik"? I'm thinking this will NOT be replaced.... the problem with that is that I only want to replace those strings that don't have the specified substring ("%EJ%") prefixing them.... something that just has a percent should get replaced.

I hope I'm making sense here....
Post Reply