[Solved] Regexp. How to extract values from xml document [closed]


If it really looks like this:

<myid>1234</myid>

…you can extract it like this:

Matcher match = Pattern.compile("<myid>(\d+)</myid>").matcher(str);

…and then use the matcher repeatedly, getting the value from the capture group.

But there’s a reason everyone is telling you to use a proper parser. There are lots of ways the above can fail, both matching inappropriately and failing to match when it should.

The proper solution is to make the XML valid, and then parse it, and use XPath or similar to read the values.

If you really have some tool requiring you to send it invalid XML, you need to replace that tool. More likely, though, it’s some misunderstanding.

1

solved Regexp. How to extract values from xml document [closed]