Hi,
irb(main):001:0> a = “test”
=> "test"
irb(main):002:0> b = “Test”
=> "Test"
irb(main):003:0> reg = Regexp.new /test/i
=> /test/
irb(main):004:0> reg.match a
=> #MatchData:0x4021b960
irb(main):005:0> reg.match b
=> nil
How do I make reg be case insensetive?
db
···
–
Apr 15 Leonardo da Vinci born, 1452
Apr 15 Lincoln dies, 1865
Apr 15 Ray Kroc opens first McDonalds in Des Plaines, IL, 1955
Apr 15 Bengali New Year in Bangladesh
Apr 15 Income Tax Day
Apr 15* Omer 18th day
Hi,
Sorry for wasting time, I found it immediatly after even though I had
looked before.
irb(main):001:0> reg = Regexp.new(/test/,Regexp::IGNORECASE)
=> /test/i
db
···
On Wed, Apr 16, 2003 at 02:12:58AM +0900, Daniel Bretoi wrote:
Hi,
irb(main):001:0> a = “test”
=> “test”
irb(main):002:0> b = “Test”
=> “Test”
irb(main):003:0> reg = Regexp.new /test/i
=> /test/
irb(main):004:0> reg.match a
=> #MatchData:0x4021b960
irb(main):005:0> reg.match b
=> nil
How do I make reg be case insensetive?
db
–
Apr 15 Leonardo da Vinci born, 1452
Apr 15 Lincoln dies, 1865
Apr 15 Ray Kroc opens first McDonalds in Des Plaines, IL, 1955
Apr 15 Bengali New Year in Bangladesh
Apr 15 Income Tax Day
Apr 15* Omer 18th day
–
Apr 15 Leonardo da Vinci born, 1452
Apr 15 Lincoln dies, 1865
Apr 15 Ray Kroc opens first McDonalds in Des Plaines, IL, 1955
Apr 15 Bengali New Year in Bangladesh
Apr 15 Income Tax Day
Apr 15* Omer 18th day
This post (below) made me wonder if you can enforce case-insensitivity
on a Regexp object (say that you received via a method).
Since Regexp.new can take a regex as its first argument, and accepts a
second arguemnt to specify options. (Some detail left out there.)
So you can do
def insensitive(regex)
Regexp.new(regex, Regexp::IGNORE_CASE)
end
However, ri tells me that when a regex is given to Regexp.new, its
options are not propagated !!
Therefore, the definition of #insensitive above is incorrect for
inputs that have Regexp::EXTENDED or Regexp::POSIX_LINE enabled, and I
can’t see any way to define it correctly.
This is not a practical issue for me. It’s just interesting that Ruby
apparently lacks this capability.
Gavin
Hi,
irb(main):001:0>> a = “test”
=>> “test”
irb(main):002:0>> b = “Test”
=>> “Test”
irb(main):003:0>> reg = Regexp.new /test/i
=>> /test/
irb(main):004:0>> reg.match a
=>> #MatchData:0x4021b960
irb(main):005:0>> reg.match b
=>> nil
···
On Wednesday, April 16, 2003, 3:12:58 AM, Daniel wrote:
How do I make reg be case insensetive?
db
Gavin,
So you can do
def insensitive(regex)
Regexp.new(regex, Regexp::IGNORE_CASE)
end
However, ri tells me that when a regex is given to
Regexp.new, its options are not propagated !!
Therefore, the definition of #insensitive above is
incorrect for inputs that have Regexp::EXTENDED or
Regexp::POSIX_LINE enabled, and I can’t see any way
to define it correctly.
It looks like this oversight has been fixed in 1.8:
%ruby -v -e “x = /x/imx; puts x.options”
ruby 1.8.0 (2003-04-04) [i386-freebsd4.7]
135
So your function could be:
def insensitive(regex)
Regexp.new(regex, regex.options | Regexp::IGNORECASE)
end
Or even:
def insensitive(regex)
if regex.options & Regexp::IGNORECASE
regex
else
Regexp.new(regex, regex.options | Regexp::IGNORECASE)
end
end
- Warren Brown