How to negate regexps in case?

Hi All!

I would like to know if it’s possible to negate regexps in
case-when-then-else-end. I.g.

line ="I need to know"
case line
when ? <!>? /^I need .*/
puts "right"
end

Best regards,
Slava Potsepnia

I may be all wrong, but I thought case was equivalent to:
if line === something
do_something
elseif line === something_else
do_something_else
else
do_default
end

If this is so, that “===” is implied, then the “when” cannot contain just the
regex. This is klunky but works:

def nomatch(str, regex)
if str !~ regex
str
else
nil
end
end
line = "I nyeed to know"
regex = %r{^I need.*}
case line
when regex
puts "a match"
when nomatch(line, regex)
puts "no match"
end

···

On Wednesday 21 August 2002 11:12 am, Slava Potsepnia wrote:

Hi All!

I would like to know if it’s possible to negate regexps in
case-when-then-else-end. I.g.

line ="I need to know"
case line
when ? <!>? /^I need .*/
puts "right"
end

Best regards,
Slava Potsepnia

You can often use the negative lookahead operator to simulate that
effect, as in:

case line
when /^(?!regex)/ then # expression does not occur at start of line

when /\A(?!.*regex)/ then # expression does not occur anywhere

end

[…]

		Reimer Behrends
···

Slava Potsepnia (ViacheslavPotsepnia@scnsoft.com) wrote:

Hi All!

I would like to know if it’s possible to negate regexps in
case-when-then-else-end. I.g.

Thanks, Reimer. I really need to learn more regex. I’m keeping this email.

···

On Wednesday 21 August 2002 05:13 pm, Reimer Behrends wrote:

Slava Potsepnia (ViacheslavPotsepnia@scnsoft.com) wrote:

Hi All!

I would like to know if it’s possible to negate regexps in
case-when-then-else-end. I.g.

You can often use the negative lookahead operator to simulate that
effect, as in:

case line
when /^(?!regex)/ then # expression does not occur at start of line

when /\A(?!.*regex)/ then # expression does not occur anywhere

end

[…]

  	Reimer Behrends