Reputation: 2928
Example email: ex@mpleEm@[email protected]
Expected result: ex@mpleEm@il
How do I do this?
Upvotes: 2
Views: 1554
Reputation: 97688
The simplest solution is probably to use a POSIX regular expression match:
select substring('ex@mpleEm@[email protected]' from '(.*)@');
There are two slightly subtle things going on here:
.*
matches as much as possible, which is why it includes the extra @
s (compare select substring('ex@mpleEm@[email protected]' from '(.*?)@');
which has a "non-greedy" modifier)substring
function returns the contents of the first capture if there are any capturing parentheses, which is why it doesn't include the final @
(compare select substring('ex@mpleEm@[email protected]' from '.*@');
which has no capturing parentheses)Upvotes: 3
Reputation: 121524
Use substring(string from pattern):
select substring('ex@mpleEm@[email protected]' from '(.*)@');
substring
--------------
ex@mpleEm@il
(1 row)
Upvotes: 4