I have the following XML:
<t>a_35345_0_234_345_666_888</t>
I would like to replace the first occurrence of number after "_" with a fixed number 234. So the result should look like:
<t>a_234_0_234_345_666_888</t>
I have tried using the following but it does not work:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<xsl:value-of select='replace(., "(.*)_\d+_(.*)", "$1_234_$2")'/>
</xsl:template>
</xsl:stylesheet>
UPDATE
The following works for me (thanks @Chris85). Just remove the underscore and add "? to make it non greedy.
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<xsl:value-of select='replace(., "(.*?)_\d+(.*)", "$1_234$2")'/>
</xsl:template>
</xsl:stylesheet>
Your regex is/was greedy, the .*
consumes everything until the last occurrence of the next character.
So
(.*)_\d+_(.*)
was putting
a_35345_0_234_345_666_
into $1
. Then 888
was being removed and nothing went into $2
.
To make it non-greedy add a ?
after the .*
. This tells the *
to stop at the first occurrence of the next character.
Functional example:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<xsl:value-of select='replace(., "(.*?)_\d+(.*)", "$1_234$2")'/>
</xsl:template>
</xsl:stylesheet>
Here's some more documentation on repetition and greediness, http://www.regular-expressions.info/repeat.html.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With