Literal '-' in regular expression bracket sets

From: Steve <s-psql(at)rhythm(dot)cx>
To: pgsql-general(at)postgresql(dot)org
Subject: Literal '-' in regular expression bracket sets
Date: 2002-09-05 18:41:03
Message-ID: 20020905144103.A30720@infinity.rhythm.cx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2002-09-05 18:50:55 Re: Literal '-' in regular expression bracket sets
Previous Message Mario Weilguni 2002-09-05 18:39:55 Re: PostgreSQL vs MySQL : strange results on insertion