Regex questions

Lähettäjä: E S <eero.saynatkari@kolumbus.fi>
Aihe: Re: regex questions

> Lähettäjä: Jeff Davis <jdavis-list@empires.org>
> Aihe: regex questions
>
> In python the regexes allow you to call a function instead of just
> substitute the values (see <http://docs.python.org/lib/node111.html&gt; for
> more details). That seems quite useful, is there something similar in ruby?
>
> Also, let's say I want match anything between "a" and "b" unless it
> contains the word "foo". I could write two regexes like so:
>
> if str =~ /a(.*)b/ and str !~ /a(.*foo.*)b/
>
> Is there a good way to make that kind of logic into one regex? Is there
> some kind of "intersect" operator or a "not" operator?

There's /(?!foo)/x, but I can't think of any proper way to get it to
accept all other strings but the one specified. You could just
invert your problem and reject any strings that *do* have a 'foo'
between.

unless str =~ /a.*?foo.*?b/

If you need to, you can group to extract the non-foo part there.

That didn't come out right. What I meant to write is that if the above
match is not enough and you need to know that 'foo' is present, you
can simply do this:

str =~ /a.*?(foo).*?b/
fail if $1

···

> Regards,
> Jeff Davis

E