RE [xquery-talk] using a variable within a regexp pattern

Hans-Juergen Rennau hrennau at yahoo.de
Thu Oct 1 22:13:38 PDT 2009


Hi Manual,

if I understand you correctly, you wonder how you can compose a match pattern from variable and fixed  parts, e.g. framing a variable term by an initial ^ and a trailing $, right?

The solution is simple: use an _expression_ whose string value is the desired sequence of characters. Often this will be achieved using the concat function, whose arguments in turn may be literals, variable references, function calls, path expressions.... Examples: 

matches(., concat('^', $q, '$'))
matches(., concat($prefix, $q, $postfix))
matches(., concat('^', 'ID', position()))
matches(., concat('^', $n/@criterion, '.*s'))

So the task becomes very simple if you remember that the way an expression is resolved to a value is independent on the context where the value is used. Therefore, you need not any special knowledge about the matches function - all you need is the general understanding how to build an expression that resolves to the intended string.

With kind regards,
Hans-Juergen

------------------------------

Message: 4
Date: Thu, 1 Oct 2009 20:53:29 +0200
From: Manuel Souto Pico <m.soutopico at gmail.com>
Subject: [xquery-talk] using a variable within a regexp pattern in
    XQuery
To: talk at x-query.com
Message-ID:
    <8bb717a90910011153j4e4821f9t29f927fa39d7df35 at mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi,

I'm a bit stuck trying to run a regexp pattern in a XQuery search file.

(1) where $record/langSet[@xml:lang="en"]/tig/term[matches(., $q)]
works
(2) where $record/langSet[@xml:lang="en"]/tig/term[matches(.,
"$q")]            doesn't work
(3) where $record/langSet[@xml:lang="en"]/tig/term[matches(.,
"^$q.*$")]         doesn't work
(4) where $record/langSet[@xml:lang="en"]/tig/term[matches(.,
"^bu.+$")]          works!

Any ideas about how I can get to use regular expressions in this query?

(4) retrieves 'bufer', 'bug' and 'bus'. If $q is 'bu', (1) retrieves as well
'attri*bu*te', 'contrast *bu*tton', etc.

Thanks a lot!

Cheers, Manuel


      



More information about the talk mailing list