Q: n-times matching

Hi,

I wrote the following code, but I can’t get an expeceted result.
…===================== nmatch.rb ========
str = 'hogeratta’
while str =~ /<(/?\w+)>/ do
print $1, "\n"
end
…========================================

Expected output:
…----------------------------------------
$ ruby nmatch.rb
html
body
/body
/html
…----------------------------------------

Real output:
…----------------------------------------
% ruby nmatch.rb
html
html
html
… (infinite loop)
…----------------------------------------

Please tell me how to get what I expected.

regards,
makoto

Hi,

I wrote the following code, but I can’t get an expeceted result.
…===================== nmatch.rb ========
str = ‘hogeratta’
while str =~ /<(/?\w+)>/ do
print $1, “\n”
end
…========================================

It matches at same point everytime.

str.scan(/<(/?\w+)>/) do
print $1, “\n”
end

pos = 0
while pos = str.index(/<(/?\w+)>/, pos) do
print $1, “\n”
pos += $1.size
end

pos = 0
while str =~ /\A.{#{pos}}.*?<(/?\w+)>/ do
print $1, “\n”
pos = $&.size
end

···

At Mon, 17 Jun 2002 11:16:02 +0900, kwatch wrote:


Nobu Nakada

Thank you very much, Nobu.

You showed three ways to make my desire.
Which is the fastest? Please tell me if you know it.

regards
kwatch

nobu.nokada@softhome.net wrote in message news:200206170318.g5H3Ib211677@sharui.nakada.kanuma.tochigi.jp

···

It matches at same point everytime.

str.scan(/<(/?\w+)>/) do
print $1, “\n”
end

pos = 0
while pos = str.index(/<(/?\w+)>/, pos) do
print $1, “\n”
pos += $1.size
end

pos = 0
while str =~ /\A.{#{pos}}.*?<(/?\w+)>/ do
print $1, “\n”
pos = $&.size
end

Hi,

You showed three ways to make my desire.
Which is the fastest? Please tell me if you know it.

I ordered in faster first.

String#index needs to adjust multibyte alignment each time, and
it’s slow in 1.6. And the last example need to create new
Regexps each time.

And errata.

pos = 0
while pos = str.index(/<(/?\w+)>/, pos) do
while str.index(/<(/?\w+)>/, pos) do
print $1, “\n”
pos += $1.size
pos = $~.end(0)
end

pos = 0
while str =~ /\A.{#{pos}}.?<(/?\w+)>/ do
while str =~ /\A.{#{pos}}.
?<(/?\w+)>/m do
print $1, “\n”
pos = $&.size
pos = $~.end(0)

···

At Tue, 18 Jun 2002 04:37:11 +0900, kwatch wrote:

end


Nobu Nakada

Thank you very much.
I adopt your first example.

regards,
kwatch

nobu.nokada@softhome.net wrote in message news:200206180254.g5I2sl227739@sharui.nakada.kanuma.tochigi.jp

···

Hi,

At Tue, 18 Jun 2002 04:37:11 +0900, > kwatch wrote:

You showed three ways to make my desire.
Which is the fastest? Please tell me if you know it.

I ordered in faster first.

String#index needs to adjust multibyte alignment each time, and
it’s slow in 1.6. And the last example need to create new
Regexps each time.

And errata.

pos = 0
while pos = str.index(/<(/?\w+)>/, pos) do
while str.index(/<(/?\w+)>/, pos) do
print $1, “\n”
pos += $1.size
pos = $~.end(0)
end

pos = 0
while str =~ /\A.{#{pos}}.?<(/?\w+)>/ do
while str =~ /\A.{#{pos}}.
?<(/?\w+)>/m do
print $1, “\n”
pos = $&.size
pos = $~.end(0)
end