Simple regexp question

Hi

In ruby, I can use the following to match a regexp:

if string=~/pattern/ then …

However, the following does not work,

if string!~/pattern/ then…

I’ll have to write

if !(string=~/pattern/) then…

Any suggestion?

Shannon

Hi

In ruby, I can use the following to match a regexp:

if string=~/pattern/ then …

However, the following does not work,

if string!~/pattern/ then…

I’ll have to write

if !(string=~/pattern/) then…

Any suggestion?

Shannon

Funny that. My copy of “Ruby in a Nutshell” says that ‘!~’ is an operator
method, so I’m surprised that it’s not defined for String. I had no luck
defining it myself either.

My suggestion is:
unless string =~ /pattern/ then …

Gavin

···

From: “Shannon Fang” xrfang@hotmail.com

Hi,

···

In message “simple regexp question” on 02/11/28, Shannon Fang xrfang@hotmail.com writes:

However, the following does not work,

if string!~/pattern/ then…

I guess “string !~ /pattern/” does.

						matz.

Since method names can include !, this gets read as
if string! ~ /pattern/
and fails because there’s no Object#string!

Both the following work:

if string !~ pattern
if “hello world”!~ pattern

martin

···

Shannon Fang xrfang@hotmail.com wrote:

Hi

In ruby, I can use the following to match a regexp:

if string=~/pattern/ then …

However, the following does not work,

if string!~/pattern/ then…

I’ll have to write

if !(string=~/pattern/) then…

Perhaps your pattern isn’t doing what you think it should or the string
is different from what you expect. The following works for me on ruby 1.6.7

if( ‘one’ !~ /two/ )
puts ‘it works!’
end

If you post more information (what is your pattern and some example strings)
we might be able to help more.

  • alan
···

On Thu, Nov 28, 2002 at 04:09:21PM +0900, Shannon Fang wrote:

However, the following does not work,

if string!~/pattern/ then…


Alan Chen
Digikata Computing
http://digikata.com

Hi

In ruby, I can use the following to match a regexp:

if string=~/pattern/ then …

However, the following does not work,

if string!~/pattern/ then…
This is read as ‘string! ~/pattern/’, so ruby tries to call method ‘!’
on object string, taking the argument ‘~ /pattern/’ (~ == bitwise
negation).

I’ll have to write

if !(string=~/pattern/) then…

Any suggestion?

Use spaces around operators :slight_smile:

···

On Thu, Nov 28, 2002 at 04:09:21PM +0900, Shannon Fang wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Oh, I’ve seen copies [of Linux Journal] around the terminal room at The Labs.
– Dennis Ritchie

if !(string=~/pattern/) then…

Since method names can include !, this gets read as
if string! ~ /pattern/
and fails because there’s no Object#string!

Not to mention that adding a bit of whitespace around binary
operators such as these generally increases code readability as
well.

···

=====

Yahoo IM: michael_s_campbell


Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.