0

I have string xml like below:

<Query>
  <Code>USD</Code>
  <Description>United States Dollars</Description>
  <UpdateTime>2013-03-04 02:27:33</UpdateTime>
  <toUSD>1</toUSD>
  <USDto>1</USDto>
  <toEUR>2</toEUR>
  <EURto>3</EURto>
</Query>

All text is in one line without white spaces. I can't write right regex pattern. I want get nodes which begin like <to. For example <toEUR>, <toUSD>.

How should I write this pattern?

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194

2 Answers2

0

With nokogiri and the xpath function starts-with:

require 'nokogiri'
doc = Nokogiri::XML <<EOF
<Query>
  <Code>USD</Code>
  <Description>United States Dollars</Description>
  <UpdateTime>2013-03-04 02:27:33</UpdateTime>
  <toUSD>1</toUSD>
  <USDto>1</USDto>
  <toEUR>2</toEUR>
  <EURto>3</EURto>
</Query>
EOF

doc.search('//*[starts-with(name(),"to")]').map &:to_s
#=> ["<toUSD>1</toUSD>", "<toEUR>2</toEUR>"]
pguardiario
  • 53,827
  • 19
  • 119
  • 159
-1

Although the general consensus is that parsing xml etc with regex is not the way to go, something like this should do the trick:

<\s*(to[^>\s]+)[^>]*>([^<]+)<\s*/\s*\1\s*>

In ruby format:

/<\s*(to[^>\s]+)[^>]*>([^<]+)<\s*\/\s*\1\s*>/

Matches <toWatever>value</toWhatever> back-reference group 1 returns the name (toWhatever) and back-reference group 2 returns the value.

rvalvik
  • 1,559
  • 11
  • 15