1

I want to replace strings between @ and . in email address when selecting from PostgreSQL.

so far I can replace @ and everything after it

select REGEXP_REPLACE(contactemail, '@(.*)(\.)?', '@xxx') from customer;
2
  • SELECT REGEXP_REPLACE('[email protected]', '@[^\.]+\.', '@xxx.') -> [email protected] Commented Nov 5, 2019 at 12:10
  • Your second version works well, it replaces everything between @ and first dot. Thank you Commented Nov 5, 2019 at 12:23

1 Answer 1

1

Your @(.*)(\.)? pattern matches a @, then captures into Group 1 any 0+ chars other than line break chars as many as possible, and then tries to match an optional dot, i.e. this (\.)? will never match any text but empty. It is no wonder you match all after @ with the pattern.

You need to use

SELECT REGEXP_REPLACE(col, '@[^.]*\.', '@xxx.')

Or, if you want to only remove the part of text between the closest @ and .:

SELECT REGEXP_REPLACE(col, '@[^@.]*\.', '@xxx.')

The pattern matches

  • @ - a @ char
  • [^.]* - zero or more occurrences of any char but .
  • \. - a dot.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.