EXSLT - regexp:match - Version 1

Version: 1
Status: new
User Page: index.html
XML Definition: regexp.match.xml
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
      

An implementation of this extension function in the EXSLT regexp namespace must conform to the behaviour described in this document.

Implementations

Built-in support for regexp:match is available in the following XSLT processors:

ProcessorProcessor VersionImplemented Version
4XSLT, from 4Suite.0.12.0a31

The following implementations of regexp:match are available:

LanguageImplemented VersionCreatorDateDownload
Javascript1Chris Bayes2001-06-24regexp.match.js
Msxsl1Chris Bayes2001-06-24regexp.match.msxsl.xsl

Use Cases

Use Case Package: regexp.match.use-cases.zip

The following use cases illustrate the functionality of regexp:match.

DetailsCalling StylesheetXML DataExpected ResultCategoryTemplate
1regexp.match.1.xslregexp.match.data.1.xmlregexp.match.result.1.xmlexampleN

Change History

Submitted: 2001-06-24
Creator: Chris Bayes(http://www.bayes.co.uk)

Returns the nodes that match a regular expression.
VersionModifiedByDetails
1.12002-08-20Craig Stewart

Added 4XSLT implementation to the list.

1.22002-11-12Craig Stewart

Updated 4XSLT version to 0.12.0a3.

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