Re: [xsl] Is there a way in XSLT/XPath to find the portion of a string that matches a pattern?

Subject: Re: [xsl] Is there a way in XSLT/XPath to find the portion of a string that matches a pattern?
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 11 Mar 2025 22:25:49 -0000
On 11/03/2025 23:12, Roger L Costello costello@xxxxxxxxx wrote:
Liam gave this wicked cool way to return the portion of a string that
matches a regex pattern:



replace($input, '^.*(\d+).*$', '$1')




However, I did some testing, and it's not returning the desired results:



<xsl:variable name="TEXT" select="'The person put 12 dollars into the
jar'"/>

<xsl:variable name="INTEGER" select="'[0-9]+'"/>


<xsl:value-of select="replace($TEXT,'^.*(' || $INTEGER || ').*$', '$1')"/>



Result: 2 <-- should be 12



Another test:



<xsl:variable name="TEXT" select="'SATSET'"/>

<xsl:variable name="VOWELS" select="'A|E|I|O|U'"/>

<xsl:value-of select="replace($TEXT,'^.*(' || $VOWELS || ').*$', '$1')"/>



Result: E <-- should be A



What am I doing wrong?




I guess you need non-greedy matching for the .* with .*? e.g. replace($TEXT,'^.*?(' || $INTEGER || ').*$', '$1')

Current Thread