Re: What's wrong with this regexp? - Mailing list pgsql-general

From merlyn@stonehenge.com (Randal L. Schwartz)
Subject Re: What's wrong with this regexp?
Date
Msg-id 86d44uoors.fsf@blue.stonehenge.com
Whole thread Raw
In response to What's wrong with this regexp?  (Nick <nboutelier@gmail.com>)
List pgsql-general
>>>>> "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

pgsql-general by date:

Previous
From: Tim Landscheidt
Date:
Subject: Re: What's wrong with this regexp?
Next
From: "Massa, Harald Armin"
Date:
Subject: Re: How to send multiple SQL commands from Python?