If you want to use a regexp in the condition, the only way to do it is to have the condition be a regexp, that will be applied to the tag itself (ie you can't apply it to the whole path):

#!/usr/bin/perl use strict; use warnings; use XML::Twig; XML::Twig->new( twig_handlers => { qr/^tag[1-3]/ => sub { print $_->ta +g, ": ", $_->text, "\n"; } }) ->parse( \*DATA); __DATA__ <result> <target type="aim"> <tag1>123</tag1> <tag2>456</tag2> <nottag>789</nottag> </target> </result>

It would be nice to at least be able to apply the regexp to the path, so you could write qr{/result/target/tag[1-3]}. I'll look into it. Further than that, I don't think XML::Twig can do better, at least as it is currently implemented. Allowing the "xpath-like" interpreter to deal with XPath regexp syntax (start-with and the likes) would be a bit difficult, and Perl's regexp syntax and the XPath syntax collide ( [...] is a character class for Perl and a predicate for XPath), so not much hope there.


In reply to Re: XML::Twig and handles on regex/xpath by mirod
in thread XML::Twig and handles on regex/xpath by Eythil

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.