user2423959
user2423959

Reputation: 834

string pattern not matching correctly

I've the below XML piece of code

<page>U1/68/5, U1/73/2, V1/65B/43, V5/4E/8E</page>

and i'm trying the below XSLT.

<xsl:template match="page"> 
      <xsl:analyze-string select="." regex="([a-zA-Z][0-9]+)/([(0-9)]+[a-zA-Z]{0,1})/([(0-9)]+[a-zA-Z]{0,1})">
          <xsl:matching-substring>
            <a
              href="{concat('er:#HKWBV3_PT_',regex-group(1),'/',regex-group(1),'-',regex-group(2),'-',regex-group(3))}">
              <xsl:value-of select="."/>
            </a>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>    
</xsl:template>

When i'm using the above XSLT, <xsl:non-matching-substring> block is getting called and when i tried the same REGEX in regexpal.com, the condition was accepting. please let me know where am i going wrong in my REGEXmatch.

Thanks

Upvotes: 1

Views: 203

Answers (1)

Jongware
Jongware

Reputation: 22457

As I suspected, {0,1} is the cause of the problem -- but not for the reason I thought!

http://www.w3.org/TR/xslt20/#analyze-string:

Note:

Because the regex attribute is an attribute value template, curly brackets within the regular expression must be doubled. For example, to match a sequence of one to five characters, write regex=".{{1,5}}". For regular expressions containing many curly brackets it may be more convenient to use a notation such as regex="{'[0-9]{1,5}[a-z]{3}[0-9]{1,2}'}", or to use a variable.

Since here you only use {0,1} you can use a single ? instead, but of course that's not always an option.

Upvotes: 3

Related Questions