Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda