Hi, again, Rubyists.
Below is code that does multiple substitutions on a block of
text, using a recursive call to a regexp. The output is like:
s1 = {\aa TEXT:} It is {\bb amazing} what a {\xx regexp} can do
s2 = TEXT: It is amazing what a
regexp can do
The trouble is I am getting the following warning (repeated 3 times)
warning: string pattern instead of regexp; metacharacters no longer
effective
when I run the code. The warning comes from the line in the code that reads
s = str.sub(match[0], ssub)
This expression works (no warnings) outside the function. Any ideas as to
why this is happening and how I can get around the warning?
BTW, is the a better way of doing this task?
Regards,
-mark.
—< code >----
def rformat(rfmt,str)
fmt = rfmt.slice(-2, 2) # take the last 2 characters only
s = ''
case fmt
when “aa” then s = "#{str}"
when “bb” then s = "#{str}"
else s = "<#{fmt}>#{str}</#{fmt}>"
end
return s
end
$re = Regexp.compile(/({\[a-z]+)\s([^}]*)(})/)
def rexp(str)
match = $re.match(str)
return str if match.nil?
ssub = rformat(match[1], match[2])
s = str.sub(match[0], ssub)
rexp(s) # recurse to find more patterns
end
s1 = '{\aa TEXT:} It is {\bb amazing} what a {\xx regexp} can do’
puts "s1 = #{s1}"
s2 = rexp(s1)
puts “s2 = #{s2}”
Hi –
Hi, again, Rubyists.
Below is code that does multiple substitutions on a block of
text, using a recursive call to a regexp. The output is like:
s1 = {\aa TEXT:} It is {\bb amazing} what a {\xx regexp} can do
s2 = TEXT: It is amazing what a
regexp can do
The trouble is I am getting the following warning (repeated 3 times)
warning: string pattern instead of regexp; metacharacters no longer
effective
when I run the code. The warning comes from the line in the code that reads
s = str.sub(match[0], ssub)
This expression works (no warnings) outside the function. Any ideas as to
why this is happening and how I can get around the warning?
I couldn’t quite get your code to run, and sort of rewrote it (see
below). But in general, that warning has to do with a recent change
in metacharacter handling in cases where an argument can be either a
string or a regex, and I’m pretty sure the warning will only exist for
one version or so of Ruby, and then be taken out.
BTW, is the a better way of doing this task?
I would use String#gsub:
subs = { “aa” => “alpha”,
“bb” => “beta” }
s1 = ‘{\aa TEXT:} It is {\bb amazing} what a {\xx regexp} can do’
s2 = s1.gsub(/{\(\S+)\s([^}]*)}/) do |s|
tag = subs[$1] || $1
“<#{tag}>#{$2}</#{tag}>”
end
puts “s1 = #{s1}”
puts “s2 = #{s2}”
David
···
On Sat, 11 Jan 2003, Mark Probert wrote:
–
David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav
David.
Many thanks, once again.
I would use String#gsub:
s2 = s1.gsub(/{\(\S+)\s([^}]*)}/) do |s|
tag = subs[$1] || $1
“<#{tag}>#{$2}</#{tag}>”
end
Ruby continues to amaze me! I didn’t know that you
could do that. A much more elegant solution than mine.
Regards,
-mark.
···
At 06:30 AM 1/11/2003 +0900, you wrote: