in reply to Help for regex

If you just need to do it once, and have data that is absolutely guaranteed to be well formed (so that the regex will be reliable) you can use something like this:

/<ID>(.*?)<\/ID>/

But if you have to do it regularly or on files where you can't guarantee that they'll be well formed, use an XML or HTML parser (e.g. XML::Parse or HTML::TokeParser). If it's not well formed XML, then an HTML parser is likely to be more forgiving.

Replies are listed 'Best First'.
Re^2: Help for regex
by Anonymous Monk on Apr 01, 2012 at 18:00 UTC
    For this kind of thing I find XML::Simple to be, well, SIMPLE! ;) Using REs for ML parsing is risky because so much is permissible in XML/SGML/HTML. You have to be concerned with character sets, entities, etc. That said, I have often done it. But do take a look at XML::Simple, which answers most "trivial" cases quite well (and is built on the more robust XML libraries, so you can move to those if you need to).