Re: What's wrong with this regexp?

From: merlyn(at)stonehenge(dot)com (Randal L(dot) Schwartz)
To: Nick <nboutelier(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: What's wrong with this regexp?
Date: 2009-10-10 22:41:27
Message-ID: 86d44uoors.fsf@blue.stonehenge.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>>>>> "Nick" == Nick <nboutelier(at)gmail(dot)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(at)stonehenge(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Massa, Harald Armin 2009-10-11 00:48:39 Re: How to send multiple SQL commands from Python?
Previous Message Tim Landscheidt 2009-10-10 22:30:50 Re: What's wrong with this regexp?