Another easy one

My brain just isn’t working this morn…

Say, ch is a character and I want to find if it
exists in a regexp /A-Za-z0-9_/. How do I do it?

     > c.include?(/A-Za-z0-9_/)
     "TypeError: cannot convert Regexp into String"

I can do:

     > c.count("A-Za-z0-9_")
     1

And look for a 0 being a non-match.

Help, please! :slight_smile:

-mark.

Don’t know if it is possible to use include? here,
but you can do this with “=~”:

c =~ /[A-Za-z0-9_]/

Returns 0 if true, nil if false.
HTH.

···

On Tue, Sep 24, 2002 at 01:09:45AM +0900, Mark Probert wrote:

Say, ch is a character and I want to find if it
exists in a regexp /A-Za-z0-9_/. How do I do it?

    > c.include?(/A-Za-z0-9_/)
    "TypeError: cannot convert Regexp into String"


[ Wojtek gminick Walczak ][ http://gminick.linuxsecurity.pl/ ]
[ gminick (at) hacker.pl ][ gminick (at) underground.org.pl/ ]

Hi –

···

On Tue, 24 Sep 2002, Mark Probert wrote:

My brain just isn’t working this morn…

Say, ch is a character and I want to find if it
exists in a regexp /A-Za-z0-9_/. How do I do it?

     > c.include?(/A-Za-z0-9_/)
     "TypeError: cannot convert Regexp into String"

I can do:

     > c.count("A-Za-z0-9_")
     1

And look for a 0 being a non-match.

Try:

/\w/.match(ch)

David


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

In article 5.1.0.14.2.20020923113822.00b0cea8@zcard04k.ca.nortel.com,
Mark Probert wrote:

My brain just isn’t working this morn…

Say, ch is a character and I want to find if it
exists in a regexp /A-Za-z0-9_/. How do I do it?

     > c.include?(/A-Za-z0-9_/)
     "TypeError: cannot convert Regexp into String"

I can do:

     > c.count("A-Za-z0-9_")
     1

And look for a 0 being a non-match.

Is c a one character string? If so you can say

%w(a ^ x).each { |c|
puts “#{c} matches” if c =~ /[A-Za-z0-9_]/ # /\w/ maybe
puts “#{c} matches” if c.count(“A-Za-z0-9_”).nonzero?
}

Hope this helps, (and that I haven’t missed your point :slight_smile:

Mike

···


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

Hi,

As has been pointed out, I think you were mixing string, regular
expression and array. If you want to use regexp, you can write

c =~ /[A-Za-z0-9_]/    # will give you the char pos or nil

If you want to use array, you can write

[('A'..'Z').to_a, ('a'..'z').to_a, (0..9).to_a,

‘_’].flatten.include? c # will give you true or false

And if you want to use string, you wrote it correctly:

c.count("A-Za-z0-9_")    # will give you the number of occurences

Regards,

Bill

···

===========================================================================
Mark Probert probertm@nortelnetworks.com wrote:

My brain just isn’t working this morn…

Say, ch is a character and I want to find if it
exists in a regexp /A-Za-z0-9_/. How do I do it?

     > c.include?(/A-Za-z0-9_/)
     "TypeError: cannot convert Regexp into String"

I can do:

     > c.count("A-Za-z0-9_")
     1

And look for a 0 being a non-match.

Help, please! :slight_smile:

-mark.

My brain just isn’t working this morn…

Ask ri :slight_smile:

Say, ch is a character and I want to find if it
exists in a regexp /A-Za-z0-9_/. How do I do it?

     > c.include?(/A-Za-z0-9_/)
     "TypeError: cannot convert Regexp into String"

% ri ‘String#include?’
-------------------------------------------------------- String#include?
str.include? aString → true or false
str.include? aFixnum → true or false

···

At Tue, 24 Sep 2002 01:09:45 +0900, Mark Probert wrote:

 Returns true if str contains the given string or character.
    "hello".include? "lo"   #=> true
    "hello".include? "ol"   #=> false
    "hello".include? ?h     #=> true

%

I can do:

     > c.count("A-Za-z0-9_")
     1

And look for a 0 being a non-match.

% ri String#count
----------------------------------------------------------- String#count
str.count( [aString]+> ) → aFixnum

 Each aString parameter defines a set of characters to count. The
 intersection of these sets defines the characters to count in str.
 Any aString that starts with a caret (^) is negated. The sequence
 c1--c2 means all characters between c1 and c2.
    a = "hello world"
    a.count "lo"            #=> 5
    a.count "lo", "o"       #=> 2
    a.count "hello", "^l"   #=> 4
    a.count "ej-m"          #=> 4

%

Other way of doing it:

c[/[A-Za-z0-9_]/]

for example:

a = “blah”
if a[/([A-Za-z0-9_])/] # true
print $1,“\n”
end

if a[/([!@%])/] # false
print $1,“\n”
end

···

On Tue, Sep 24, 2002 at 02:37:04AM +0900, gminick wrote:

On Tue, Sep 24, 2002 at 01:09:45AM +0900, Mark Probert wrote:

Say, ch is a character and I want to find if it
exists in a regexp /A-Za-z0-9_/. How do I do it?
Don’t know if it is possible to use include? here,
but you can do this with “=~”:
c =~ /[A-Za-z0-9_]/
Returns 0 if true, nil if false.

[ Wojtek gminick Walczak ][ http://gminick.linuxsecurity.pl/ ]
[ gminick (at) hacker.pl ][ gminick (at) underground.org.pl/ ]

That was my original thought:

$ cat blah.rb
c = “C”
case c
when c =~ /[A-Za-z0-9_]/
puts “yip!”
else
puts “nope…”
end

$ ruby !$
ruby blah.rb
nope…

$ ruby -v
ruby 1.7.2 (2002-07-02) [i386-mswin32]

-mark.

···

At 02:37 AM 9/24/2002 +0900, gminick wrote:

On Tue, Sep 24, 2002 at 01:09:45AM +0900, Mark Probert wrote:

Say, ch is a character and I want to find if it
exists in a regexp /A-Za-z0-9_/. How do I do it?

    > c.include?(/A-Za-z0-9_/)
    "TypeError: cannot convert Regexp into String"

Don’t know if it is possible to use include? here,
but you can do this with “=~”:

c =~ /[A-Za-z0-9_]/

Returns 0 if true, nil if false.
HTH.

Thanks for all the help, everyone. I can go back to sleep now. :slight_smile:

Regards,

-mark.

···

At 02:58 AM 9/24/2002 +0900, Mike wrote:

In article 5.1.0.14.2.20020923113822.00b0cea8@zcard04k.ca.nortel.com,
Mark Probert wrote:

My brain just isn’t working this morn…

Is c a one character string? If so you can say

c =~ /[A-Za-z0-9_]/
Returns 0 if true, nil if false.
That was my original thought:
$ cat blah.rb
c = “C”
case c
when c =~ /[A-Za-z0-9_]/
puts “yip!”
else
puts “nope…”
end

$ ruby !$
ruby blah.rb
nope…

···

On Tue, Sep 24, 2002 at 03:21:34AM +0900, Mark Probert wrote:

a = “abc”
b = “#@%”

if a =~ /[A-Za-z0-9_]/
puts “found!”
else
puts “not found… :/”
end

if b =~ /[A-Za-z0-9_]/
puts “found!”
else
puts “not found… :/”
end

% ruby rb.rb
found!
not found… :confused:
% _

works for me :wink:

$ ruby -v
ruby 1.7.2 (2002-07-02) [i386-mswin32]
% ruby -v
ruby 1.6.7 (2002-03-01) [i586-linux]
% _

still 1.6.7… :wink:


[ Wojtek gminick Walczak ][ http://gminick.linuxsecurity.pl/ ]
[ gminick (at) hacker.pl ][ gminick (at) underground.org.pl/ ]

Hi,

I think you just had an extra syntax there.

c = “C”
case c
when /[A-Za-z0-9_]/ # no “c =~” here
puts “yip!”
else
puts “nope…”
end

Regards,

Bill

···

============================================================================
Mark Probert probertm@nortelnetworks.com wrote:

That was my original thought:

$ cat blah.rb
c = “C”
case c
when c =~ /[A-Za-z0-9_]/
puts “yip!”
else
puts “nope…”
end

$ ruby !$
ruby blah.rb
nope…

$ ruby -v
ruby 1.7.2 (2002-07-02) [i386-mswin32]

-mark.

puts $1

–Gavin

···

----- Original Message -----
From: “gminick” gminick@underground.org.pl

print $1,“\n”

On a case statement, when foo' does equality check by foo.===. In your code this means (Fixnum or nil) === c’, that’s not your
intension, is it?

Case matching against Regexp should be written:

case c
when /[A-Za-z0-9_]/
  puts "match"
else
  puts "nope!"
end

where /[A-Za-z0-9_]/.===, that’s mere alias of Regexp#=~, will be
called.

···

In message 5.1.0.14.2.20020923141722.04f2ba40@zcard04k.ca.nortel.com probertm@nortelnetworks.com writes:

$ cat blah.rb
c = “C”
case c
when c =~ /[A-Za-z0-9_]/
puts “yip!”
else
puts “nope…”
end

$ ruby !$
ruby blah.rb
nope…

$ ruby -v
ruby 1.7.2 (2002-07-02) [i386-mswin32]


kjana@dm4lab.to September 24, 2002
No gains without pains.