-1

please, advice how to get xml attribute value with regexp python style.

Here is the example, the extract has to be full match(long story)

<method code="ABC">

the most I achieved is (?<=code=\")(.*?)(?=\">), but that ignores method part at all

thanks

Nikolay Marinov
  • 101
  • 1
  • 3
  • 17

1 Answers1

0

Well, either you learn the basics of regex syntax and use re, or you have a look at the Python's xml.etree.ElementTree module to parse the xml directly.

If you want to go with a regex, you can test language-specific expressions interactively with online tools, regex101 for example.

Maybe <(?P<method>\w*?) (?P<code>\w*?)=\"(?P<value>.*)\"> will do the job. But once again, as mentioned, extracting data from XML using regex is fragile and very limited.


EDIT: Clearer explanation for 3

EDIT2: Add regex suggestion

FabienP
  • 3,018
  • 1
  • 20
  • 25
  • By saying python style, I meant that the application that will process this regular expression expects python regexp syntax. Coding is not an option here, so the only way is to get regular expression working. – Nikolay Marinov Aug 18 '17 at 17:08
  • Then you can use the link I provided at the end. It is an online tool that allow to test language specific regex. I will edit my answer so it is clearer. – FabienP Aug 18 '17 at 18:54
  • Thanks for your answer Fabien, I've already did test my regexp on the site you suggested before posting here and asking for solution – Nikolay Marinov Aug 18 '17 at 20:23
  • Basic regex suggestion added, you can work on this base. – FabienP Aug 18 '17 at 20:40