Search code examples
xslt-2.0regex-groupxpath-2.0xslt-3.0

Using replace() instead of xsl:analyze-string for a regex XSLT 2 3


I have an xsl:analyze-string() with a regular expression that works as desired.

<xsl:analyze-string select="'file:/D:/workspace/projects/Original/img/star.jpg'" regex="([^.]*$)">
   <xsl:matching-substring>
      <xsl:value-of select="regex-group(1)"/>
   </xsl:matching-substring>
 </xsl:analyze-string>

Returns the correct result:

jpg

However, instead of using xsl:analyze-string() I would like to use replace() (this gives an error: The regular expression must not be one that matches a zero-length string)

<xsl:value-of select="replace('file:/D:/workspace/projects/Original/img/star.jpg', '([^.]*$)', '$1')"/>

I have another analyze-string expression, this one works as a replace():

<xsl:value-of select="replace('file:/D:/workspace/projects/Original/img/star.jpg', '^.*Original/(.*)$', '$1')"/>

It returns img/star.jpg which is the desired result.


Solution

  • I don't think the * is the only obstacle preventing your attempt to work, I think you rather want e.g. replace('file:/D:/workspace/projects/Original/img/star.jpg', '.*?([^.]+)$', '$1'), i.e. for replace you have to match on more than the part you want to output.