veeresh yh veeresh yh - 4 months ago 8
Ruby Question

Extract values after pattern in Ruby string

I have a string like this:

"<root><some ProdCode=\"40\" ProducerName=\"demo1\" ProdCode=\"40\" Need_Confirmation=\"1\"/><some ProdCode=\"40\" ProducerName=\"demo1\" ProdCode=\"40\" Need_Confirmation=\"1\"/></root>"


I'm trying to pull the content from this string which is between =\"content\" and put it in an array, like ["40","demo1","40","1",40......]

Answer

You should use :scan to select elements by regexp pattern. Then remove escape characters.

string.scan(/"[^"]+"/).map { |element| element.delete('\\"')  }

Explanation of pattern:

  1. / – regexp starts
  2. " – first char should be "
  3. [^"]+ – next should be any char except ". + sign says that number of such chars should be at least 1.
  4. " – next should be again "
  5. / – regexp ends

So string.scan(/"[^"]+"/) would return:

["\"40\"", "\"demo1\"", "\"40\"", "\"1\"", "\"40\"", "\"demo1\"", "\"40\"", "\"1\""]

Then we can just delete \" using :delete method.

Convenient tool to build regexps is http://rubular.com/