[xquery-talk] regexp help

Michael Dyck jmdyck at ibiblio.org
Tue Dec 21 16:06:23 PST 2004


pooja singh wrote:
> 
>           I am trying to write a regular expression that match a
>           string that has only digits but no letter or anything
>           else.
> 
>           For example 12.6 Days should not match in the regular
>           expression
> 
>           But 1100 should match . There can be upto 15 digits in
>           the string
> 
>           if (fn:matches($fileNumber/text(),"\d[0..15]")) then
>
>           The above regular expression matches 12.3 Days also

First, you should understand the difference between square brackets and
curly braces. The regex "\d[0..15]" matches:
   -- a digit
   followed by
   -- a character in the group '0..15', i.e. a zero, one, five, or dot.
In "12.6 Days", it matches the "2."
In "1100", it matches "11"

Instead, what you presumably meant was "\d{0,15}", which matches a
sequence of zero to 15 digits.
In "12.6 Days", it would match "12".
In "1100", it would match "1100".

The problem then is that fn:matches() returns true if it can match the
regex *anywhere* in the subject string, whereas you want "\d{0,15}" to
match the *whole* subject string. The way to achieve this is to add a
caret and dollar-sign to the regex:
    "^\d{0,15}$"
This matches:
   -- the beginning of the subject string
   followed by
   -- zero to 15 digits
   followed by
   -- the end of the subject string
Then, the only way that fn:matches() can succeed is if the whole subject
string is digits.
In "12.6 Days", matching fails at the ".".
In "1100", it matches the whole string.

-Michael



More information about the talk mailing list