xsl:analyze-string
Splits a string into matching and non-matching substrings using a regular expression, and processes each piece separately.
<xsl:analyze-string select="$input" regex="\d+">
<xsl:matching-substring>...</xsl:matching-substring>
<xsl:non-matching-substring>...</xsl:non-matching-substring>
</xsl:analyze-string>The input string is scanned left to right. Each match of regex triggers xsl:matching-substring; the text between matches (and before the first / after the last) triggers xsl:non-matching-substring. Either child may be omitted. Inside xsl:matching-substring, regex-group($n) returns the captured groups, with regex-group(0) being the entire match.
position() and last() reflect the position of the current segment within the full sequence of matching and non-matching segments.
Attributes
| Attribute | Description |
|---|---|
select |
XPath expression for the input string. Required. |
regex |
Regular expression. AVT. Required. |
flags |
Regex flags as an AVT (i, s, m, x). |
Examples
Tagging numbers in a sentence:
<xsl:analyze-string select="'I have 3 apples and 12 oranges'" regex="\d+">
<xsl:matching-substring>
<num><xsl:value-of select="."/></num>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>Extracting key-value pairs with capture groups:
<xsl:analyze-string select="$line" regex="(\w+)=(\S+)">
<xsl:matching-substring>
<pair key="{regex-group(1)}" value="{regex-group(2)}"/>
</xsl:matching-substring>
</xsl:analyze-string>