Thread: What's wrong with this regexp?

What's wrong with this regexp?

From
Nick
Date:
SELECT TRUE WHERE '/steps/?step=10' ~ '^\/steps\/\?step=10$'

Im guessing its an escape issue, but where am I going wrong?

Re: What's wrong with this regexp?

From
Tim Landscheidt
Date:
Nick <nboutelier@gmail.com> wrote:

> SELECT TRUE WHERE '/steps/?step=10' ~ '^\/steps\/\?step=10$'

> Im guessing its an escape issue, but where am I going wrong?

You need to double-escape the question mark: Once for the
string literal, once for the regular expression (and you do
not need to escape the slashes). This gives:

| tim=# SELECT TRUE WHERE '/steps/?step=10' ~ E'^/steps/\\?step=10$';
|  bool
| ------
|  t
| (1 Zeile)

| tim=#

Tim

Re: What's wrong with this regexp?

From
merlyn@stonehenge.com (Randal L. Schwartz)
Date:
>>>>> "Nick" == Nick  <nboutelier@gmail.com> writes:

Nick> SELECT TRUE WHERE '/steps/?step=10' ~ '^\/steps\/\?step=10$'

Here's the first clue:

    merlyn=# select '^\/steps\/\?step=10$';
    WARNING:  nonstandard use of escape in a string literal
    LINE 1: select '^\/steps\/\?step=10$';
                   ^
    HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
         ?column?
    -------------------
     ^/steps/?step=10$
    (1 row)

Notice the \'s just disappeared, so it's not gonna have much good
for the ?, which will be interpreted as the "optional" suffix.

Even adding 'E' (from the hint) isn't quite enough:

    merlyn=# select E'^\/steps\/\?step=10$';
         ?column?
    -------------------
     ^/steps/?step=10$
    (1 row)

We need the resulting value to have \? in it, and that's not
there yet.  So, that's the clue.  Don't need \/, but do need \\?, so
it looks like this:

    merlyn=# select E'^/steps/\\?step=10$';
          ?column?
    --------------------
     ^/steps/\?step=10$
    (1 row)

Aha, and now we have the right string for the regex engine, so
let's test that match:

    merlyn=# select '/steps/?step=10' ~ E'^/steps/\\?step=10$';
     ?column?
    ----------
     t
    (1 row)

Bingo.  True.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion