|
Checking if String.scan does not match: msg#02293ruby-talk
Hi everyone, I'm extracting 3 words from a string delimited with colons using the following approach: str = "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 only 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 = "test:string" matched = false str.scan(/(.+):(.+):(.+)/) do |a, b, c| puts a, b, c matched = 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
|
|
||||||||||||||||||||||||||
|
|
|
| News | Mail Home | sitemap | FAQ | advertise |