Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
Matches only at the end of the string

I'd like to protect the email field of the following table:

CREATE TABLE users (
  email VARCHAR(255) NULL CHECK (email ~* '\A[^@]+@[^@]+\Z')
);

What I would like to do is to allow strings such as:

bob@example

But I would like to avoid strings such as:

bob@example\nfuu

I heard that the \Z constraint allows any chars after another line (with \n).

According to best practices in regex, the \z is better than \Z as it allow only one line, but it seems to be not supported by PostgreSQL. And the $ is not better.

Am I true?

Edit:

I tested this:

CREATE TABLE users (
  email VARCHAR(255) NULL CHECK (email ~* '\A[^@\n]+@[^@\n]+\Z')
);

CREATE UNIQUE INDEX users__lower_case__email ON users(lower(email));

--

INSERT INTO users (email) VALUES ('\nfoo\n@\nbar\n');

Apparently the constraint didn't work: the wrong email was added in the table.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Note that negated character classes match any characters but those defined in the set. So, [^@] matches any chars but @, including newline symbols. To exclude a newline, just add it to the class.

Use

email ~* '\A[^@\n]+@[^@\n]+\Z'

As \Z only matches only at the end of the string there is no way this regex could allow a newline in the input.

over 4 years ago · Santiago Trujillo Report

0

I would highly suggest you review my post for the right way to store an E-Mail address in PostgreSQL.

  • What is the best way to store an email address in PostgreSQL?

Here is some example code,

CREATE EXTENSION citext;
CREATE DOMAIN email AS citext
  CHECK ( value ~ '^[a-zA-Z0-9.!#$%&''*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$' );

SELECT 'asdf@foobar.com'::email;

For your table,

CREATE TABLE users (
  user_email  email
);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!