Wokoman
Wokoman

Reputation: 1119

some help needed with regular expression (xslt 2)

I have following xml example :

<test>
<p>Some text (page 24)</p>
<p>Some text  (Page 24)</p>
<p>there is some text here (page 24) and here (page 25)</p>
<p>some text (pages 24, 7, 9)</p>
</test>

and I want to use some regex to wrap all page text snippets into a page_ref tag, so result would look like this :

<test>
<p>Some text <page_ref>(page 24)</page_ref></p>
<p>Some text <page_ref>(Page 24)</page_ref></p>
<p>there is some text here <page_ref>(page 24)</page_ref> and here <page_ref>(page 25)</page_ref></p>
<p>some text <page_ref>(pages 24, 7, 9)</page_ref></p>
</test>

my code looks as follows

<xsl:template match="text()">
    <xsl:analyze-string select="." regex="\(([pP]age).*\)">
        <xsl:matching-substring>
            <page_ref><xsl:value-of select="."/></page_ref>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
            <xsl:value-of select="."/>
        </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>

but it fails on my 3d p tag, as it produces

<p>there is some text here <page_ref>(page 24) and here (page 25)</page_ref></p>

So if someone is able to point me the error I'll be a happy guy. Thanks in advance !

Upvotes: 1

Views: 73

Answers (2)

Eric
Eric

Reputation: 2116

You could try matching any character except for ), up to the next ).

"\(([pP]age)[^)]*\)"

Upvotes: 1

Rui Jarimba
Rui Jarimba

Reputation: 17994

Try this regular expression:

\(pages?\s+\d+(,\s*\d+)*\)

PS: This was tested using .NET regular expressions, but it should work with XSLT as well

Upvotes: 1

Related Questions