Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 16th, 2007, 05:05 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default Regular Expression Question

Hi,

I am currently trying to analyze a string for a pattern, but I dont think I have the concept right. I am looking for a pattern like:

[\d{2}]\-[\d{2}]\-[\d{2}]

00-00-00

I am able to return the string which cotains the pattern, however I want to be able to just return the pattern.. e.g

21-00-00

Code:
<xsl:variable name="numbers" as="xs:string*">
                                    <xsl:analyze-string select="." regex="[\d{2}]\-[\d{2}]\-[\d{2}]">
                                        <xsl:matching-substring>
                                            <xsl:sequence select="."/>
                                        </xsl:matching-substring>
                                    </xsl:analyze-string>
                                </xsl:variable>
                                <xsl:variable name="all" select="replace(.,'([^0-9\.][^0-9\.][^-])','')"/>
                                <xsl:value-of select="$all"/>

I can't figure out this line

<xsl:variable name="all" select="replace(.,'([^0-9\.][^0-9\.][^-])','')"/>
 
Old April 16th, 2007, 09:25 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I think your variable $numbers is OK, except that the regex attribute is an AVT and therefore all curly braces need to be doubled - a popular gotcha.

I'm afraid I can't figure out your $all variable either. I've no idea what you're trying to achieve with this.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 17th, 2007, 06:08 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Hi Michael,

Thanks for the guidance on AVT - curly brackets...

As for the regex:

I am trying to extract a number that has this pattern:

##-##-##

However the following replace() extracts all the numbers, even if they do not match the pattern:

<xsl:variable name="all" select="replace(.,'[^\d\-]','')"/>

"The policy number 21-00-00 is in section 1"

would return:

21-00-001

Perfered output:

21-00-00


Thanks for the help.

 
Old April 17th, 2007, 06:33 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Well,

'([^0-9\.][^0-9\.][^-])'

means "a character that isn't a digit or dot, followed by a character that isn't a digit or dot, followed by a character that isn't a hyphen".

So if you apply that to

"The policy number 21-00-00 is in section 1"

Then it will first match "The", then " po", then "lic", then "y n", then "umb", then "er " (by chance the number of characters before the 21-00-00 is a multiple of three), then " is", then " in", then " se", then "cti", then "on ". It doesn't match the final "1" so that is left in your string after the replacement (the same would be true if the final "1" were "A").

So you've definitely got the concept wrong!

I'm not sure why you don't like the analyze-string solution, but if you want to do this using replace, and if there's only one occurrence of the dd-dd-dd in your input string, then you can use

replace($in, '^.*(\d\d-\d\d-\d\d).*$', '$1')


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 17th, 2007, 06:58 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Hi Michael,

I took your suggestion and used the analyze-string method. Not escaping the curly brackets was the problem.

Thanks again!







Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression Help Please rstelma ASP.NET 1.0 and 1.1 Professional 0 January 2nd, 2008 07:01 PM
Regular Expression NewToXSL ASP.NET 1.0 and 1.1 Basics 1 June 13th, 2006 02:52 PM
Regular expression question - non-capturing groups holdmykidney J2EE 0 July 22nd, 2005 10:52 AM
Regular expression question cridley General .NET 1 May 28th, 2005 01:14 PM
question regarding regular expression validator NewbieProg Classic ASP Basics 1 November 9th, 2004 08:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.