View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Jason Lee > Text-XmlMatch > Text::XmlMatch



Annotate this POD


New  2
Open  0
View/Report Bugs
Module Version: 1.0006   Source  


Text::XmlMatch - Pattern-matching and grouping via XML configuration file


 use Text::XmlMatch;
 my $matcher = Text::XmlMatch->new('ConfigurationFile.xml');

 #Find group, results returned as hash reference
 my $results = $matcher->findMatch('');
 foreach (keys %$results) {
   print "Group Name\t--   Group Type \n";
   print "$_\t--   $$results{$_} \n";

 Sample XML Configuration "ConfigurationFile.xml":

  <!-- Find FQDN's that match a particular datacenter -->
  <pattern name="DATACENTER-ndc">

  <!-- Find devices that match a particular market -->
  <pattern name="Market CSC">


This module provides matching/grouping functions via a configuration file specified in XML format. By specifying inclusion criteria and pattern names, the user may pass strings to the created object to perform sophisticated pattern matching/grouping. In addition, optional exclusion criteria may be specified as well as an optional descriptor to further refine the behavior of the searching and the returned search results.

This grouping and classification function is required frequently in network management systems, where hundreds and often thousands of items need to be grouped according to a variety of criteria. Such grouping can be discreet or overlapping depending on the configuration specified. In complex management systems where multiple platforms are required, this module can ease administrative burdens by allowing multiple systems to share a common configuration file. Each system can then be configured to only respond to items matching a specific pattern name.

In addition, this module allows for dynamic group name creation via support of back-references. By following the convention of Perl's memory variables, grouping can be accomplished such that a pattern name depends on the content of what is being matched. All of this behavior is determined by way of a simple XML configuration file.



Using the XML configuration file that was specified during the Text::XmlMatch object creation, it will return all matches in the form of a hash reference. The keys of this hash are the pattern names that correspond to matches in the XML configuration file. The values of the hash contain the tag information (if any was specified, otherwise it simply contains the value '0').

A "match" for a pattern name in this module implies the following are both true:


This simply returns a an array or reference to an array containing a list of all the pattern names that were derived from the XML configuration file. The caller's context determines whether an array or reference is returned.

XML Configuration file ^

The format for the XML configuration file is as follows:

   <pattern name="group_name_goes_here">
config tag

A mandatory tag that specifies the start of the XML configuration file. Any valid configuration file for this module must open with this tag.

pattern name tag

A mandatory tag that specifies the name for the group that is to be established. Any submitted strings that match the criteria specified for this pattern will return this name as described when findMatch() is called.

Note that this opening tag must include at least one inclusion tag (see below). The following keywords are reserved and must not be used as a pattern name: 'inclusion', 'tag', 'name'.

inclusion tag

A tag that contains a regular expression. At least one of these must be defined within the pattern name tags, but multiple regular expressions can be specified by including multiple <inclusion> tags. Note, if multiple inclusion tags are specified, they are treated as a logical OR. If any string submitted via findMatch() matches any one of the regular expressions identified by an inclusion cause, then that string is considered a match for the group if and only if the string does not match a regular expression identified within an exclusion tag (see below).

exclusion tag

This is an optional tag that contains a regular expression. Multiple regular expressions can be specified by including multiple exclusion tags. Note, if multiple exclusion tags are specified, they are treated as a logical OR. For a given pattern name, a string that matches any regular expression contained within an exclusion set will cause pattern name to not be returned by findMatch().

tag tag

Other than having an unfortunate choice of name, this provides an optional descriptor for each pattern section. As an example, if one wants to establish pattern name "types," the tag section could be set so that all matches can be further categorized later. The user would then have the option of using the results of the individual patterns along with more sophisticated grouping based on the returned tags.

XML Configuration with Back-references ^

Using up to five memory variables: $1, $2, $3, $4, and $5, the XML configuration can be configured to provide dynamic pattern name creation. By using standard memory parentheses in the <inclusion> tags, the memory variables may be directly referenced in pattern name. For example, take the following configuration:

 <!-- Standard COID based facility name -->
  <pattern name="COID-$1">

The results of the match are captured in the memory parentheses (\d{5}) will be present in the final group name of "COID-$1," where $1 will be replaced by the results of the match.


Configuration files that contain duplicated pattern names will cause undesired behavior. Instead of specifying a pattern name more than once, consider using multiple <inclusion> tags under a single pattern name, or create multiple Text::XmlMatch objects pointing to different configuration files.


This module requires XML::Simple 2.14.


The extras directory contains sample XML configuration files that are also used to provide the configurations for the test scripts.


Jason A. Lee <>


 Text::XmlMatch version 1.0006

 Copyright 2007, Jason Lee
   All rights reserved.
syntax highlighting: