Обсуждение: Literal dash in regular expression brackets

Поиск
Список
Период
Сортировка

Literal dash in regular expression brackets

От
Steve
Дата:
I'm trying to place a literal '-' in a bracketed character set in a regular
expression for a check constraint. I am currently escaping it with a '\',
however, it still winds up in the table definition as a non-literal dash and
is interpreted as a character range. For instance:

CREATE TABLE retest
(
    hostname VARCHAR(100) CHECK (hostname ~ '^[a-zA-Z0-9\-\.]+$' )
);

works (in the psql utility). Then if I do

INSERT INTO retest(hostname) VALUES ('asdf.com');

psql says

ERROR:  Invalid regular expression: invalid character range in [ ]

If I look at the table definition, the regex reads as '^[a-zA-Z0-9-.]+$'. So
how do I put a literal '-' in the bracket set? Backslashing doesn't seem to
work. Is the '.' being interpreted too? The '.' is supposed to be a literal
'.' as well.

Thanks


Re: Literal dash in regular expression brackets

От
Thomas O'Dowd
Дата:
Hi Steve,

Just put the dash first or last :)

'^[a-zA-Z0-9.-]+$'

or better still

'^[[:alnum:].-]+$'

Tom.

On Fri, 2002-09-06 at 03:39, Steve wrote:
>
> I'm trying to place a literal '-' in a bracketed character set in a regular
> expression for a check constraint. I am currently escaping it with a '\',
> however, it still winds up in the table definition as a non-literal dash and
> is interpreted as a character range. For instance:
>
> CREATE TABLE retest
> (
>     hostname VARCHAR(100) CHECK (hostname ~ '^[a-zA-Z0-9\-\.]+$' )
> );
>
> works (in the psql utility). Then if I do
>
> INSERT INTO retest(hostname) VALUES ('asdf.com');
>
> psql says
>
> ERROR:  Invalid regular expression: invalid character range in [ ]
>
> If I look at the table definition, the regex reads as '^[a-zA-Z0-9-.]+$'. So
> how do I put a literal '-' in the bracket set? Backslashing doesn't seem to
> work. Is the '.' being interpreted too? The '.' is supposed to be a literal
> '.' as well.
>
> Thanks
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
--
Thomas O'Dowd. - Nooping - http://nooper.com
tom@nooper.com - Testing - http://nooper.co.jp/labs


Re: Literal dash in regular expression brackets

От
Bruno Wolff III
Дата:
On Thu, Sep 05, 2002 at 14:39:26 -0400,
  Steve <steve@infinity.rhythm.cx> wrote:
>
> I'm trying to place a literal '-' in a bracketed character set in a regular
> expression for a check constraint. I am currently escaping it with a '\',
> however, it still winds up in the table definition as a non-literal dash and
> is interpreted as a character range. For instance:
>
> CREATE TABLE retest
> (
>     hostname VARCHAR(100) CHECK (hostname ~ '^[a-zA-Z0-9\-\.]+$' )
> );

It should be:
     hostname VARCHAR(100) CHECK (hostname ~ '^[-a-zA-Z0-9.]+$' )