Marcos Placona
Marcos Placona
Developer evangelist at Twilio, GDE and in ❤️ with Open Source
1 min read


  • Regular Expressions

As most of you already know, I LOVE regular expressions, and think they are great to solve simple and complex tasks involving strings.

One thing I usually need to do, is extract contents from inside HTML or XML tags.

Take the following XML document for example:

    <title>empire burlesque</title>
    <artist>bob dylan</artist>
    <title>hide your heart</title>
    <artist>bonnie tyler</artist>
    <company>cbs records</company>
    <title>greatest hits</title>
    <artist>dolly parton</artist>

Now, let’s say

we want to return all the titles contained within the tags, but not return the tags themselves.

An accepted pattern would be writte

n like:


The concept is very simple, we’re saying we want to match one or more characters after the tag. we then add a “?” to make our regular expression non-greedy (regular expressions are greedy by default, and wi

ll try and find the last occurrence of what’s being searched).

Now, if you run the aforementioned pattern against the XML string on your favourite language (I chose ruby here as it’s got the method scan that does exactly what I need), you should get an array with all the titles on the xml.

xml_re = /<title>(.+?)</title>/m
m = strXML.scan(xml_re)
print m

Nifty eh?