Jeff Davis wrote:
> In python the regexes allow you to call a function instead of just
> substitute the values (see <http://docs.python.org/lib/node111.html>
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?


You can't write executable code within the regex, but...

When constructing the regex you can include calls that will be
evaluated, just like double-quote string:

start_tag = 'a'
end_tag = 'b'
r = /#{start_tag}(.*?)#{end_tag}/  #=> /a(.*?)b/

Also, if you call sub/gsub you can pass a block. E.g.

s1 = 'xxx a x foo x  b xxx'
r = /a(.*?)b/
puts s1.sub(r) { |match| match =~ /foo/ ? '' : match } #=> 'xxx  xxx'
HTH,
Assaph