Project

Profile

Help

Regex issue

Added by Anonymous over 15 years ago

Legacy ID: #6646702 Legacy Poster: Frank Ploessel (fploessel)

Hi, There seems to be a bug in the Saxon 9.1.0.5J regex engine: fn:replace('(a)-(b)-c', '^(([^\)]+)-)([^\(][^\)])$', '$1($2)') returns (b)-(c) instead of (a)-(b)-(c). The latter should be returned as the pattern is anchored with ^ to the begin of the input string. Hence $1, content of the parenthesis pair immediately following the ^, should start at the begin of the input string if it has any content at all. As Saxon does not seem to support the Perl 5 syntax for grouping, but not catching parentheses, the workaround fn:replace('(a)-(b)-c', '^(?:([^\)]+)-)([^\(][^\)])$', '($1)') does not work, so I will have to find another workaround, maybe avoiding regexes at all and use some looping and standard string functions. Frank


Replies (2)

RE: Regex issue - Added by Anonymous over 15 years ago

Legacy ID: #6648117 Legacy Poster: Michael Kay (mhkay)

^ matches the start of the string (([^\)]+)-)* matches first "(a)-", then "(b)-" ([^\(][^\)]*) matches (c) $ matches the end of the string. So both "(a)-" and "(b)-" are matched by the first capturing group, but $1 refers to the last match of the first group, which is "(b)-" >As Saxon does not seem to support the Perl 5 syntax for grouping Saxon supports the XPath 2.0 syntax for regular expressions, not the Perl 5 syntax.

RE: Regex issue - Added by Anonymous over 15 years ago

Legacy ID: #6657297 Legacy Poster: Frank Ploessel (fploessel)

Hi Michael, Sorry, looking at the expression more closely this morning, I found that what I wanted to achieve needs the following regex syntax: fn:replace('(a)-(b)-c', '^((([^\)]+)-))([^\(][^\)])$', '$1($3)') i. e. catching everything from the start of the string up to and including the last minus in $1. And this works correctly in Saxon. > Saxon supports the XPath 2.0 syntax for regular expressions, not the Perl 5 syntax. Yes, I agree, but in all documents and books describing the XPath regex syntax, you find a reference to Perl. So I thought it might be worth a try, bearing in mind that it might or might not work, and did not start a search to try to find the exact specification of XPath regexes. By the way, both the wrong and the correct syntax of my regex give the same result in Saxon as in Perl. Frank

    (1-2/2)

    Please register to reply