'Pass in reg ex as variable

I would like to pass in a regular express pattern through a variable i.e. as below:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:math="http://www.w3.org/2005/xpath-functions/math"
            xmlns:map="http://www.w3.org/2005/xpath-functions/map"
            xmlns:array="http://www.w3.org/2005/xpath-functions/array"
            exclude-result-prefixes="#all"
            version="3.0">

            <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

            <xsl:template match="/">

                    <xsl:variable name="pattern" as="xs:string" select ="'[0-9]{4}'"/>

                    <xsl:variable name="caseSeries" >
                        <xsl:analyze-string select="'asdasd1980asdasd'" regex="$pattern">
                             <xsl:matching-substring>
                                  <xsl:value-of select="."/>
                             </xsl:matching-substring>
                        </xsl:analyze-string>
                    </xsl:variable>

                   <result>
                      <areaCode> 
                           <xsl:value-of select="$caseSeries"/>
                      </areaCode>
                  </result>

            </xsl:template>

</xsl:stylesheet>

but it's not matching anything, I tried escaping the curly bracket but it doesn't work. I'm planning on having an if statement to get the appropriate regex into a variable and apply that regex, so this is a POC in a way.



Solution 1:[1]

adding curly braces for the variable does the trick:

<xsl:analyze-string select="'asdasd1980asdasd'" regex="{$pattern}">

I updated the link to work

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David