-3

My xml:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

I need a regex to extract "Don't forget me this weekend!" in the tag and also if the tag exists in python using regex.

I have written a code, but I am not able to figure out the regex expression.

James Z
  • 12,209
  • 10
  • 24
  • 44
Addy
  • 23
  • 1
  • 2
  • 10
  • I think you should take a look into regex's and then come with doubts about the regex you have, if any – dcg Feb 17 '18 at 00:13
  • I think you should include your code. – Jongware Feb 17 '18 at 00:19
  • [Why “Can someone help me?” is not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – wwii Feb 17 '18 at 00:51
  • 1
    Are you sure regex is the correct tool? It's usually not the correct way to parse xml or html. – James Z Feb 17 '18 at 07:50

1 Answers1

1

A basic solution:

import re  


data  = """
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>"""

found = re.findall('<body>(.*)</body>', data)

if found:
  for x in found:
    print(x)

>> Don't forget me this weekend!
jrjames83
  • 901
  • 2
  • 9
  • 22