Reputation: 661
As best I can see from the XSLT 2.0 docs, or at least from what I read at these places:
the only way to use a regular expression to extract a specific group from some input is to use analyze-string(). Given that the replace() function is very concise by comparison, is there nothing equally concise for extraction?
For example, given the input: "ABCD: 123, DEFGH", I would like to extract just "123" and store it in a variable. Note the input can be more complex so I do want to take advantage of regular expressions and grouping.
tokenize()
is not the answer, IMO, because it seems more suited to when the input has repeating delimiters.
I was searching for a function that is as concise as replace()
and might work like this:
<xsl:variable name="num" select="extract('.', '(.+):\s*(\d+), (.+)', '$2')"/>
but I cannot find one. It seems I must use analyze-string() like I do below but it is just so lengthy:
<xsl:variable name="num">
<xsl:analyze-string select="." regex="(.+):\s*(\d+), (.+)">
<xsl:matching-substring>
<xsl:value-of select="regex-group(2)"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
Is it actually necessary to use analyze-string()
or am I missing something?
Upvotes: 2
Views: 970
Reputation: 5432
The replace()
is exactly that you are looking for. matches()
is the function used for comparison, and replace()
for extraction. It just works fine if you substitute extract
with replace
in your example
The XSLT transformation doesn't change your input. It transforms your input into an output. Your input will be intact.
Upvotes: 1
Reputation: 117073
replace('ABCD: 123, DEFGH', '(.+):\s*(\d+), (.+)', '$2')
returns:
123
Upvotes: 1