1
0
mirror of https://github.com/alexandrev/xslt-lab.git synced 2026-09-18 19:43:16 +00:00
Files
xslt-lab/site/content/xslt/functions/xpath-regex-group.md
T
alexandrev-tibco 53e90ef86e feat(blog): complete XSLT/XPath reference — 229 function pages
Full coverage of XSLT 1.0, 2.0 and 3.0 elements and XPath functions:
- 59 XSLT elements (xsl:stylesheet → xsl:use-accumulators)
- 170 XPath functions (1.0 node/string/numeric/boolean, 2.0 sequence/
  date/QName/string, 3.0 HOF/map/array/JSON/streaming)
Each page: description, parameters table, return value, 2 runnable
Saxon examples with input XML + stylesheet + output, notes, cross-links.
xsltCompletions.js: all 229 entries now have blogSlug for hover links.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 09:28:05 +02:00

3.9 KiB

title, description, date, version, versionLabel, category, syntax, tags
title description date version versionLabel category syntax tags
regex-group() Returns the string matched by a numbered capture group of the current regex match inside xsl:analyze-string. 2026-04-18T00:00:00Z 2.0 XSLT 2.0 string function regex-group(group-number)
xslt
reference
xslt2
xpath

Description

regex-group() is available exclusively inside the body of xsl:matching-substring. It returns the substring captured by the parenthesized group identified by the integer argument:

  • regex-group(0) — the entire matched string.
  • regex-group(1) — the first parenthesized capture group.
  • regex-group(2) — the second parenthesized capture group, and so on.

If the group number does not exist in the regex, or if the group did not participate in the match (e.g., it belongs to an unmatched alternative), the function returns an empty string "".

regex-group() has no meaning outside xsl:matching-substring and should not be called in other contexts.

Parameters

Parameter Type Required Description
group-number xs:integer Yes The capture group index: 0 for the full match, 1+ for numbered groups.

Return value

xs:string — the text captured by the specified group, or "" if the group did not participate in the match.

Examples

Extracting date components

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<events>
  <event>Launch on 2026-04-18 at HQ</event>
  <event>Review on 2026-05-01 remotely</event>
</events>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/events">
    <dates>
      <xsl:for-each select="event">
        <xsl:analyze-string select="." regex="(\d{{4}})-(\d{{2}})-(\d{{2}})">
          <xsl:matching-substring>
            <date year="{regex-group(1)}"
                  month="{regex-group(2)}"
                  day="{regex-group(3)}"
                  full="{regex-group(0)}"/>
          </xsl:matching-substring>
        </xsl:analyze-string>
      </xsl:for-each>
    </dates>
  </xsl:template>
</xsl:stylesheet>

Output:

<dates>
  <date year="2026" month="04" day="18" full="2026-04-18"/>
  <date year="2026" month="05" day="01" full="2026-05-01"/>
</dates>

Parsing key-value pairs

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<config>color=blue;size=large;weight=12kg</config>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/config">
    <settings>
      <xsl:analyze-string select="." regex="([a-z]+)=([^;]+)">
        <xsl:matching-substring>
          <setting key="{regex-group(1)}" value="{regex-group(2)}"/>
        </xsl:matching-substring>
      </xsl:analyze-string>
    </settings>
  </xsl:template>
</xsl:stylesheet>

Output:

<settings>
  <setting key="color" value="blue"/>
  <setting key="size" value="large"/>
  <setting key="weight" value="12kg"/>
</settings>

Notes

  • Groups are numbered by counting opening parentheses ( left to right in the regex, starting from 1.
  • Non-capturing groups (?:...) are not supported in XPath regex; all parenthesized groups capture.
  • If a group is part of an alternation that did not match (e.g., (a)|(b) matched on b — group 1 returns ""), the function returns "" rather than raising an error.
  • regex-group() is a context function; its value changes for each match iteration of xsl:analyze-string.

See also