Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2a19c2d

Browse files
authored
func email_parse() improved
1 parent 30311b3 commit 2a19c2d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

‎functions/email_parse.sql‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
create or replace function email_parse(
1+
create or replace function public.email_parse(
22
email text,
33
username out text,
44
domain out text
@@ -11,11 +11,12 @@ create or replace function email_parse(
1111
parallel safe
1212
language sql
1313
set search_path = ''
14+
cost 5
1415
as
1516
$$
1617
-- https://en.wikipedia.org/wiki/Email_address
1718
select t[1] as username, t[2] as domain
18-
from regexp_match(email, '^(.+)@([^@]+)$', '') as t
19+
from regexp_match(email, '^(.+)@([^@\s]+)$', '') as t
1920
where octet_length(email) between 6 and 320
2021
and position('@' in email) > 1 --speed improves
2122
and octet_length(t[1]) <= 64 and octet_length(t[2]) <= 255;
@@ -26,9 +27,10 @@ do $$
2627
begin
2728
--positive
2829
assert (select username = '111@222' and domain = 'ya.ru'
29-
from email_parse('111@222@ya.ru') as t);
30+
from public.email_parse('111@222@ya.ru') as t);
3031
--negative
31-
assert email_parse('123@') is null;
32-
assert email_parse('@123') is null;
32+
assert public.email_parse('123@') is null;
33+
assert public.email_parse('@123') is null;
34+
assert public.email_parse('do@it. com') is null;
3335
end;
3436
$$;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /