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=>"(.*?)" 

working demo

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

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -