ruby - Using regular expression to get values from a string -
this question has answer here:
i have string in database...
"[{:@error=>"invalid information sent: ''. check attribute correct input value."}, {:@error=>"invalid phone number: "}, {:@error=>"invalid address: "}]"
i parse out strings inside @error=>
appears.
something similar
- "invalid information sent: ''. check attribute correct input value."
- "invalid phone number: "
- "invalid address: "
from previous example tried use this...
string.scan(/'([^']+)'/).flatten.map{ |msg| msg.gsub(/(\.|\s+)/, ' ').strip }
but returned empty array.
you can use simple regex this:
@error=>"(.*?)"
match information
match 1 1. [12-91] `invalid information sent: ''. check attribute correct input value.` match 2 1. [106-128] `invalid phone number: ` match 3 1. [143-160] `invalid address: `
Comments
Post a Comment