I want to find all nested pattern matches in a string, and execute some
code at each match. This should include searching all nested substrings
of a match. I realize I could do this in perl ( I have been up to now),
but my code is getting quite complex and I would like to implement an
object model (something I could also do in perl, but works much better
in Ruby). In perl, I was able to do this in a regexp (example below).
Can I do the same thing in ruby, or will I need to use some sort of loop
to wun code at each match.
I want to find all nested pattern matches in a string, and execute some
code at each match. This should include searching all nested substrings
of a match. I realize I could do this in perl ( I have been up to now),
but my code is getting quite complex and I would like to implement an
object model (something I could also do in perl, but works much better
in Ruby). In perl, I was able to do this in a regexp (example below).
Can I do the same thing in ruby, or will I need to use some sort of loop
to wun code at each match.
As said, no code in Ruby regexps. You’ll have to do looping.
str.scan rx do |m|
doForAllSubstringsOf m do |subString|
puts subString
end
end
The funny part is implementing doForAllSubstringsOf(str). Well, it
isn’t really difficult:
def doForAllSubstringsOf(str)
len = str.length
for i in 0 … len
for j in 1 … len - i
yield str[i, j]
end
end
end
I want to find all nested pattern matches in a string, and execute some
code at each match. This should include searching all nested substrings
of a match. I realize I could do this in perl ( I have been up to now),
but my code is getting quite complex and I would like to implement an
object model (something I could also do in perl, but works much better
in Ruby). In perl, I was able to do this in a regexp (example below).
Can I do the same thing in ruby, or will I need to use some sort of loop
to wun code at each match.
As said, no code in Ruby regexps. You’ll have to do looping.
str.scan rx do |m|
doForAllSubstringsOf m do |subString|
puts subString
end
end
The funny part is implementing doForAllSubstringsOf(str). Well, it
isn’t really difficult:
def doForAllSubstringsOf(str)
len = str.length
for i in 0 … len
for j in 1 … len - i
yield str[i, j]
end
end
end