A
Alex Allmont
Hi everyone,
I'm extracting 3 words from a string delimited with colons using the follow=
ing approach:
str =3D "test:string:here"
str.scan(/(.+).+).+)/) do |a, b, c|
puts a, b, c
end
This works fine if all 3 words are present but if you pass a string with on=
ly 2 words, e.g. "test:string", the block is not executed.
I need to report an error if the match fails. This could be done like this=
:
str =3D "test:string"
matched =3D false
str.scan(/(.+).+).+)/) do |a, b, c|
puts a, b, c
matched =3D true
end
if not matched
puts "invalid format"
end
but this is messy. Is there a neater, more ruby-like, approach to this?
Any help is greatly appreciated,
Alex
I'm extracting 3 words from a string delimited with colons using the follow=
ing approach:
str =3D "test:string:here"
str.scan(/(.+).+).+)/) do |a, b, c|
puts a, b, c
end
This works fine if all 3 words are present but if you pass a string with on=
ly 2 words, e.g. "test:string", the block is not executed.
I need to report an error if the match fails. This could be done like this=
:
str =3D "test:string"
matched =3D false
str.scan(/(.+).+).+)/) do |a, b, c|
puts a, b, c
matched =3D true
end
if not matched
puts "invalid format"
end
but this is messy. Is there a neater, more ruby-like, approach to this?
Any help is greatly appreciated,
Alex