How to exclude things in REs...?!

General questions about using TextPad

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

Post Reply
Tommy Svensson

How to exclude things in REs...?!

Post by Tommy Svensson »

Hi all,

I have a very lengthy RE that finds all occurences of a string in a particular format.

Actually, what I want is TextPad to find everything that is not included by the RE above... The reason why I choose to find "the wrong" strings is that they are easier to identify. Now, my question is:

Is there a way to tell the reg exp engine that "I want you to find everything that is not included in this reg exp"... sort of a minus operator?

Thx,

/Tommy
Stephan

Re: How to exclude things in REs...?!

Post by Stephan »

I'm not sure I understand what you'd like to do...
So I assume you want to find

a) all lines that doesn't macht at all

and

b) those parts of the other lines that don't match.

Now a) is eays - I think.
For b) try to capture parts of your regex. Say <regexgoeshere> is your regex.
Then something like

^\(.?*\)<regexgoeshere>\(.*?\)$

should match those part of the lines that receed resp. follow your regex.
Anyway, for more complex regex stuff I reccommend using a regex capable language like Ruby. Write a script that does what you want and call it from within Textpad.
That works really well IMHO.

Hope that helped.

Stephan

And sorry, if part of the message appears twice. I was just too fast in hitting TAB ENTER without noticing that I didn't include a TAB into the text....
Tommy Svensson

Re: How to exclude things in REs...?!

Post by Tommy Svensson »

[Conversation post for others to see]

But how do I negate the result? That's what I'm looking for actually...

/T

----- Original Message -----
From: "Stephan Kaemper" <Stephan.Kaemper@schleswig-holstein.de>
To: <tommy@glocalnet.net>
Sent: Monday, 28 October, 2002 13:41
Subject: Re: Exclusion...


> > Well, what I want to do is this for example:
> >
> > Find all lines which contains a-z and spaces but
> > does not have the string "remove me" in it.
> >
> > What I'm looking for is a minus operator, something like this:
> >
> > ^[a-z ]+$ ~(remove me)
> >
> > /Tommy
>
> How about trying to match against
>
> ^[a-z ]*remove me[a-z ]*$
>
> and negate the result?
>
> Ruby for example provides the following construct
>
> (?!re)
>
> which matches if re does not match at the point of ocurrence in the whole regex.
> This one is a lookahead that tries to match the following string character, but dosen't 'consume' any chars if the match is successful (and fails if it's not of course).
>
> However, unless your regex is really simple (as [a-z ]* for example), you might run into runtime problems if you apply the solution above.
> The regex machine first tries to match your regex, then tries to match "remove me" and then tries to match your regex again. If there are some *'s and +'es and the like in your regex, the regex engine will likely run into considerable backtrackig.
>
> Therefore it may well be a good idea to check the lines if they match your regex or "remove me" and then check against the other. BTW, I think checking against the simple regex first in this case may well save some runtime - if I remember Friedls books correctly.
>
> Anyway, effective matching!
>
> Stephan
Fazl

Re: How to exclude things in REs...?!

Post by Fazl »

Er, sorry if this repeats what others have already said..
Anyway, a simple way (I find) to find all lines NOT MATCHING a search expression is as follows:

1) use the Find dialog and choose the Mark All button.

Tthis marks all the matching lines..

2) use the menu Search | Invert All Bookmarks.

Now all the lines that do NOT match, are bookmarked.

You can eg, copy all these using the Edit | Copy other | Bookmarked Lines.

Hth,
Fazl
Post Reply