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
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/ ]
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