EXSLT - regexp:match

Implementer Page: regexp.match.html
Function Package: regexp.match.zip

Function Syntax

object regexp:match(string, string, string?)

The regexp:match function lets you get hold of the substrings of the string passed as the first argument that match the captured parts of the regular expression passed as the second argument.

The second argument is a regular expression that follows the Javascript regular expression syntax.

The third argument is a string consisting of character flags to be used by the match. If a character is present then that flag is true. The flags are:

  • g: global match - the submatches from all the matches in the string are returned. If this character is not present, then only the submatches from the first match in the string are returned.
  • i: case insensitive - the regular expression is treated as case insensitive. If this character is not present, then the regular expression is case sensitive.

The regexp:match function returns a node set of match elements, each of whose string value is equal to a portion of the first argument string that was captured by the regular expression. If the match is not global, the first match element has a value equal to the portion of the string matched by the entire regular expression.

The following example illustrates a non-global match:

<xsl:for-each select="regExp:match('http://www.bayes.co.uk/xml/index.xml?/xml/utils/rechecker.xml', 
                                   '(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)')">
   Part <xsl:value-of select="position()" /> = <xsl:value-of select="." />
</xsl:for-each>
      

Gives the following result:

Part 1 = http://www.bayes.co.uk/xml/index.xml?/xml/utils/rechecker.xml
Part 2 = http
Part 3 = www.bayes.co.uk
Part 4 =
Part 5 = /xml/index.xml?/xml/utils/rechecker.xml
      

The following example illustrates a global match:

<xsl:for-each select="regExp:match('This is a test string', '(\w+)', 'g'">
   Part <xsl:value-of select="position()" /> = <xsl:value-of select="." />
</xsl:for-each>
      

Gives the following result:

Part 1 = This
Part 2 = is
Part 3 = a
Part 4 = test
Part 5 = string
      

The following example illustrates another global match:

<xsl:for-each select="regExp:match('This is a test string', '([a-z])+ ', 'g')">
   Part <xsl:value-of select="position()" /> = <xsl:value-of select="." />
</xsl:for-each>
      

Gives the following result:

Part 1 = his
Part 2 = is
Part 3 = a
Part 4 = test
      

The following example illustrates a global, case-insensitive match:

<xsl:for-each select="regExp:match('This is a test string', '([a-z])+ ', 'gi')">
   Part <xsl:value-of select="position()" /> = <xsl:value-of select="." />
</xsl:for-each>
      

Gives the following result:

Part 1 = This
Part 2 = is
Part 3 = a
Part 4 = test
      

Implementations

The following XSLT processors support regexp:match:

Implementations of regexp:match are available in the following languages:

Examples

Function

The following example shows how to use the regexp:match function:

Source

<?xml-stylesheet type="text/xsl" href="regexp.match.1.xsl"  ?>
<a>
   <c>Is this EXSLT? No. no</c>
</a>

Stylesheet

<xsl:import href="regexp.match.msxsl.xsl" />
<xsl:template match="c">
   <out>
      <xsl:value-of select="." />
 - 
               <xsl:for-each select="regexp:match(., 'no', 'gi')">
         <xsl:value-of select="." />
      </xsl:for-each>
   </out>
</xsl:template>

Result

<out>Is this EXSLT? No. no - 
         Nono</out>

http://www.exslt.org/regexp/functions/match/index.html last modified 2002-11-12