Thread: Regular expressions syntax: is \ the escape character ?
Hi, I'm using regular expressions in 6.5.3 version but i cannot find the character to escape special caracters as ?, $, ^. I've tried with \ but it doesn't works. Example: i want to find the row with the value 'fisica?' and i do: select title from libros where title ~* 'fisica\?' ; but i also obtain rows with values: 'fisicamente' or 'fisicas' What am i doing wrong ? Thanks in advance. Gabi :-)
You have to use two backslashes, as the preprocessor will take the first one out (I may be wrong about this, but I remember reading something similar a few months ago on this list.) Also, remember that you have to send two slashes to PostGres, which may mean that you need to use 4 slashes in your code. IE, for a PERL program: $dbi->prepare("SELECT field FROM table WHERE field ~* '\\+'"); is actually only sending: SELECT field FROM table WHERE field ~* '\+' to the backend, so you should use: $dbi->prepare("SELECT field FROM table WHERE field ~* '\\\\+'"); At 03:04 PM 3/3/00, Gabriel Fernandez wrote: >Hi, > >I'm using regular expressions in 6.5.3 version but i cannot find the >character to escape special caracters as ?, $, ^. > >I've tried with \ but it doesn't works. > >Example: i want to find the row with the value 'fisica?' and i do: > >select title from libros where title ~* 'fisica\?' ; > >but i also obtain rows with values: 'fisicamente' or 'fisicas' > >What am i doing wrong ? > >Thanks in advance. > >Gabi :-) > > >************
Re: Re: [GENERAL] Regular expressions syntax: is \ the escape character ?
From
Gabriel Fernandez
Date:
Thanks a million to you all. Finally, as someone in the list suggested, i'm using the double backslash (\\) and it seems it works ok. The only problem i have is when i want to escape a single quote (') or the backslash (\). For example: i have one row with the value 'ONE\SECOND' I try to recover it doing (from psql frontend): select field1 from table1 where field1 ~* 'ONE\\\' ; But it doesn't work, and the parser seems to be confused (it asks me to close again the quote ' ). The problem is the same with the single quote '. I've tried this: select field1 from table1 where field1 ~* 'D\\'ALEMA' ; But it doesn't work neither. Thanks for your help. Gabi :-)
Gabriel Fernandez <gabi@unica.edu> writes: > The only problem i have is when i want to escape a single quote (') or the > backslash (\). > > For example: i have one row with the value 'ONE\SECOND' > > I try to recover it doing (from psql frontend): > > select field1 from table1 where field1 ~* 'ONE\\\' ; Heh. This will probably work if you use *four* backslashes. The first time it gets parsed, 'ONE\\\\' -> 'ONE\\', because each '\\' sequence will reduce to a single '\'. Then the second time it gets parsed, the '\\' -> '\', which is what you want. (Haven't tested it, but I've seen similar things in other places.) Chris -- ---------------------------------------------------- cjones@rightnowtech.com Chris Jones System Administrator, Right Now Technologies, Inc. "Is this going to be a stand-up programming session, sir, or another bug hunt?"