Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to find words between two tags

Tags:

python

regex

How can I use regex in python to find words between tags?

s = """<person>John</person>went to<location>London</location>"""
......
.......
print 'person of name:' John
print 'location:' London 
like image 701
DevEx Avatar asked Mar 07 '14 10:03

DevEx


3 Answers

You can use BeautifulSoup for this HTML parsing.

input = """"<person>John</person>went to<location>London</location>"""
soup = BeautifulSoup(input)
print soup.findAll("person")[0].renderContents()
print soup.findAll("location")[0].renderContents()

Also, it's not a good practice to use str as a variable name in python as str() means a different thing in python.

By the way, the regex can be:

import re
print re.findall("<person>(.*?)</person>", input, re.DOTALL)
print re.findall("<location>(.*?)</location>", input, re.DOTALL)
like image 130
Sabuj Hassan Avatar answered Oct 19 '22 12:10

Sabuj Hassan


import re

# simple example
pattern = r"<person>(.*?)</person>"
string = "<person>My name is Jo</person>"
re.findall(pattern, string, flags=0)

# multiline string example
string = "<person>My name is:\n Jo</person>"
re.findall(pattern, string, flags=re.DOTALL)

This example works for simple parsing only. Have a look at python official documentation on re

To parse HTML, you should consider @sabuj-hassan answer but please remember to check this Stack Overflow gem as well.

like image 36
abrunet Avatar answered Oct 19 '22 14:10

abrunet


probably you are looking for **XML tree and elements**
XML is an inherently hierarchical data format, and the most natural way to represent it is with a tree. ET has two classes for this purpose - ElementTree represents the whole XML document as a tree, and Element represents a single node in this tree. Interactions with the whole document (reading and writing to/from files) are usually done on the ElementTree level. Interactions with a single XML element and its sub-elements are done on the Element level.

19.7.1.2. Parsing XML
We’ll be using the following XML document as the sample data for this section:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

We have a number of ways to import the data. Reading the file from disk:

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()

Reading the data from a string:

root = ET.fromstring(country_data_as_string)

Other python Xml & Html parser

https://wiki.python.org/moin/PythonXml http://docs.python.org/2/library/htmlparser.html

like image 1
Pavan Gupta Avatar answered Oct 19 '22 12:10

Pavan Gupta