gives you:use Modern::Perl; use YAPE::Regex::Explain; my $fieldname = 'SomeTag'; say YAPE::Regex::Explain->new(qr|<$fieldname>(.*?)</$fieldname>|i)->ex +plain();
But it says nothing of how inappropriate the naive approach of parsing XML with a simple regular expression is.The regular expression: (?i-msx:<SomeTag>(.*?)</SomeTag>) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?i-msx: group, but do not capture (case-insensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- <SomeTag> '<SomeTag>' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- .*? any character except \n (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- </SomeTag> '</SomeTag>' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
Using an XML parser is advised.
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
In reply to Re: regex and XML
by CountZero
in thread regex and XML
by Zapano
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |