We Recommend

Mastering Regular Expressions Mastering Regular Expressions
As this book shows, a command of regular expressions is an invaluable skill. Regular expressions allow you to code complex and subtle text processing that you never imagined could be automated. Regular expressions can save you time and aggravation. They can be used to craft elegant solutions to a wide range of problems. Once you've mastered regular expressions, they'll become an invaluable part of your toolkit. You will wonder how you ever got by without them.


Posted By

rengber on 05/16/07


Tagged

xml RegularExpression


Versions (?)


Who likes this?

5 people have marked this snippet as a favorite

cynic68
kobylinski
vali29
copyleft
wbowers


Regular Expression to Grab the Content of an XML Element


Published in: Regular Expression 


If there will be multiple sets of these tags in a scanned XML string, you need the '?' after the '*' to specify a non-greedy (lazy) match.


  1. (?<=Message\>)[\S\s]*?(?=\<\/Message)

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: Tomalak on September 4, 2007

Note that this only works with RegEx engines that support lookaround and non-greedy matching, which is not guaranteed.

If you do not have those options, you can capture the contents in parentheses: <Message>([^<]*)</Message>

In loops, this is faster than using lookaround and non-greedy matching, too.

You need to login to post a comment.