0

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.

Caroline
  • 167
  • 1
  • 11
  • Well, do you expect any input where using `replace($input, '([^.]+$)', '$1')` (i.e. with `+` instead of `*`) could give the wrong result? If you always have file names and you need that suffix then it shouldn't matter whether you match with `+` or `*`. – Martin Honnen Oct 05 '22 at 18:09
  • `([^.]+$)` works in `analyze-string`, but `` is returning the entire input string – Caroline Oct 05 '22 at 18:35

1 Answers1

2

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.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110