Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [newbie] string pattern instead of regexp

Reply
Thread Tools

[newbie] string pattern instead of regexp

 
 
Yvon Thoraval
Guest
Posts: n/a
 
      09-21-2003
I'm doing some experiment using the "rubyguide" giving this script :

#!/usr/bin/ruby
# regex.rb
# Requires an ANSI terminal!

st = "\033[7m"
en = "\033[m"

while TRUE
print "str> "
STDOUT.flush
str = gets
break if not str
str.chop!
print "pat> "
STDOUT.flush
re = gets
break if not re
re.chop!
str.gsub! re, "#{st}\\&#{en}"
print str, "\n"
end

print "\n"


when trying it i get the following warning :

pp:~/Applications/Ruby/Tutorial> ruby regex.rb
str> foobar
pat> ^fo+
regex.rb:19: warning: string pattern instead of regexp; metacharacters
no longer effective
foobar
str> abc012dbcd555
pat> \d
regex.rb:19: warning: string pattern instead of regexp; metacharacters
no longer effective
abc012dbcd555


does that means my term isn't an ANSI one or the syntax as changed ?
tuto is mostly from v 1.4 and i'm using v1.8 under MacOS X.2.6
--
Yvon
 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      09-21-2003
>>>>> "Y" == Yvon Thoraval <yvon.thoravallist@-SUPPRIMEZ-free.fr.invalid> writes:

Y> regex.rb:19: warning: string pattern instead of regexp; metacharacters
Y> no longer effective

Well, the message say that ruby will not interpret metacharacter (this is
a change in 1.. Create a Regexp object from the String

Y> re.chop!

re = Regexp.new(re.chomp)

Y> str.gsub! re, "#{st}\\&#{en}"
Y> print str, "\n"
Y> end



Guy Decoux

 
Reply With Quote
 
 
 
 
Luc Heinrich
Guest
Posts: n/a
 
      09-21-2003
Yvon Thoraval <yvon.thoravallist@-SUPPRIMEZ-free.fr.invalid> wrote:

> does that means my term isn't an ANSI one or the syntax as changed ?
> tuto is mostly from v 1.4 and i'm using v1.8 under MacOS X.2.6


As far as I know, this warning was added some times ago (in 1.7.x ?)
after a slight semantic change, to make sure users would know about it.
Before, a single character string would be treated as a string, and all
others would be converted to regexp.

The behavior is now that strings will be strings, and regexp will be
regexp. I expected this warning to go away in 1.8 but looking at ruby
code (string.c) it looks like it will go away in 1.8.1.

So in short, your gsub won't work like you expect here because you pass
it a string when you actually want a regexp.

Replace:

str.gsub! re, "#{st}\\&#{en}"

by:

str.gsub! Regexp.compile(re), "#{st}\\&#{en}"

or

str.gsub! /#{re}/, "#{st}\\&#{en}"

--
Luc Heinrich -
 
Reply With Quote
 
Yvon Thoraval
Guest
Posts: n/a
 
      09-22-2003
Luc Heinrich <> wrote:

> > does that means my term isn't an ANSI one or the syntax as changed ?
> > tuto is mostly from v 1.4 and i'm using v1.8 under MacOS X.2.6

>
> As far as I know, [...]
> So in short, your gsub won't work like you expect here because you pass
> it a string when you actually want a regexp.
>
> Replace:
>
> str.gsub! re, "#{st}\\&#{en}"
>
> by: [...]
>
> str.gsub! /#{re}/, "#{st}\\&#{en}"


it's what i've suspected, works fine now, tanxs !
--
Yvon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Issue with regexp pattern matcher withing String#gsub Craig Jolicoeur Ruby 9 04-29-2009 02:57 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
How to exclude a string using regexp pattern? Victor Java 5 07-04-2007 07:18 PM
match with string instead of regexp fails matt neuburg Ruby 2 10-11-2006 08:37 PM



Advertisments