Вводится список email-адресов в одну строчку через пробел. Среди них нужно оставить только корректно записанные адреса. Будем полагать, что к таким относятся те, что используют латинские буквы, цифры и символ подчеркивания. А также в адресе должен быть символ "@", а после него символ точки "." (между ними, конечно же, могут быть и другие символы). Результат отобразить в виде строки email-адресов, записанных через пробел. Code: Sample Input: [email protected] dfd3.ru@mail [email protected] [email protected] [email protected] Sample Output: [email protected] [email protected] [email protected] Code: from string import ascii_letters as al, digits as ds symbols = al + ds + '_' print(*filter(lambda x: '@.' < x > symbols, input().split())) Code: # мой вывод почему то пропускает 1 емаил неправильный Test input: [email protected] dfd3.ru@mail [email protected] [email protected] [email protected] Test output: dfd3.ru@mail [email protected] [email protected]
Решил Code: import re emails = input().split() res = filter(lambda x: re.findall(r'^[\w]+@[\w]+.[\w]', x), emails) print(*res)
Code: [_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,13}) корректнее, домены бывают и 3+ уровня. но по хорошему publicsuffix валидацию на то что после "@"
ну тогда уж https://fightingforalostcause.net/content/misc/2006/compare-email-regex.php isEmail по тесту вышло = 79 / 135 p.s. не особо заморачивался над эскейпами, мож какие криво экранировались Регулярками по линку выше (не тестил, copy-paste со статы) Valid: 96/134 Invalid: 130/132 PHP: <?php// https://github.com/dominicsayers/isemailrequire_once 'is_email.php';// 79 / 135// These should be valid$arrayValid = array("[email protected]", "1234567890123456789012345678901234567890123456789012345678901234@iana.org", "\"\\\"first\\\\\"last\\\"@iana.org\"", "\"\\\"first@last\\\"@iana.org\"", "\"\\\"first\\last\\\"@iana.org\"", "x@x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23 [...]", "1234567890123456789012345678901234567890123456789012345678@1234567890123456789012345678901234567890123456 [...]", "first.last@[12.34.56.78]", "first.last@[IPv6:::12.34.56.78]", "first.last@[IPv6:1111:2222:3333::4444:12.34.56.78]", "first.last@[IPv6:1111:2222:3333:4444:5555:6666:12.34.56.78]", "first.last@[IPv6:::1111:2222:3333:4444:5555:6666]", "first.last@[IPv6:1111:2222:3333::4444:5555:6666]", "first.last@[IPv6:1111:2222:3333:4444:5555:6666::]", "first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:8888]", "first.last@x23456789012345678901234567890123456789012345678901234567890123.iana.org", "[email protected]", "[email protected]", "\"\\\"first\\last\\\"@iana.org\"", "first.last@[IPv6:1111:2222:3333::4444:5555:12.34.56.78]", "first.last@[IPv6:1111:2222:3333::4444:5555:6666:7777]", "[email protected]", "first.last@com", "\"\\\"Abc\\@def\\\"@iana.org\"", "\"\\\"Fred\\ Bloggs\\\"@iana.org\"", "\"\\\"Joe.\\Blow\\\"@iana.org\"", "\"\\\"Abc@def\\\"@iana.org\"", "\"\\\"Fred Bloggs\\\"@iana.org\"", "[email protected]", "customer/[email protected]", "\[email protected]", "!def!xyz%[email protected]", "[email protected]", "[email protected]", "[email protected]", "\"\\\"Doug \\\\\"Ace\\\\\" L.\\\"@iana.org\"", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "t*[email protected]", "[email protected]", "{_test_}@iana.org", "\"\\\"[[ test ]]\\\"@iana.org\"", "[email protected]", "\"\\\"test.test\\\"@iana.org\"", "\"test.\\\"test\\\"@iana.org\"", "\"\\\"test@test\\\"@iana.org\"", "[email protected]", "[email protected]", "test@[123.123.123.123]", "[email protected]", "[email protected]", "\"\\\"test\\test\\\"@iana.org\"", "test@example", "\"\\\"test\\blah\\\"@iana.org\"", "\"\\\"test\\blah\\\"@iana.org\"", "\"\\\"test\\\\\"blah\\\"@iana.org\"", "customer/[email protected]", "[email protected]", "[email protected]", "\"\\\"Austin@Powers\\\"@iana.org\"", "[email protected]", "\"\\\"Ima.Fool\\\"@iana.org\"", "\"\\\"Ima Fool\\\"@iana.org\"", "\"\\\"first\\\".\\\"last\\\"@iana.org\"", "\"\\\"first\\\".middle.\\\"last\\\"@iana.org\"", "\"\\\"first\\\"[email protected]\"", "\"first.\\\"last\\\"@iana.org\"", "\"\\\"first\\\".\\\"middle\\\".\\\"last\\\"@iana.org\"", "\"\\\"first.middle\\\".\\\"last\\\"@iana.org\"", "\"\\\"first.middle.last\\\"@iana.org\"", "\"\\\"first..last\\\"@iana.org\"", "\"\\\"first\\\\\\\\\"last\\\"@iana.org\"", "\"first.\\\"mid\\dle\\\".\\\"last\\\"@iana.org\"", "\"\\\"test", " blah\\\"@iana.org\"", "(foo)cal(bar)@(baz)iamcal.com(quux)", "cal@iamcal(woo).(yay)com", "cal(woo(yay)hoopla)@iamcal.com", "cal(foo\\@bar)@iamcal.com", "cal(foo\\)bar)@iamcal.com", "first()[email protected]", "pete(his account)@silly.test(his host)", "c@(Chris's host.)public.example", "jdoe@machine(comment). example", "1234 @ local(blah) .machine .example", "first(abc.def)[email protected]", "\"first(a\\\"bc.def)[email protected]\"", "\"first.(\\\")middle.last(\\\")@iana.org\"", "first(abc\\(def)@iana.org", "first.last@x(1234567890123456789012345678901234567890123456789012345678901234567890).com", "a(a(b(c)d(e(f))g)h(i)j)@iana.org", "[email protected]", "a@b", "[email protected]", "aaa@[123.123.123.123]", "a@bar", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "\"\\\"hello my name is\\\"@stutter.com\"", "\"\\\"Test \\\\\"Fail\\\\\" Ing\\\"@iana.org\"", "[email protected]", "[email protected]", "[email protected]", "\"\\\"Joe\\Blow\\\"@iana.org\"", "HM2Kinsists@(that comments are allowed)this.is.ok", "user%[email protected]", "first.last @iana.org", "cdburgess+!#\$%&'*-/=?+_{}|[email protected]", "first.last@[IPv6:::a2:a3:a4:b1:b2:b3:b4]", "first.last@[IPv6:a1:a2:a3:a4:b1:b2:b3::]", "first.last@[IPv6:::]", "first.last@[IPv6:::b4]", "first.last@[IPv6:::b3:b4]", "first.last@[IPv6:a1::b4]", "first.last@[IPv6:a1::]", "first.last@[IPv6:a1:a2::]", "first.last@[IPv6:0123:4567:89ab:cdef::]", "first.last@[IPv6:0123:4567:89ab:CDEF::]", "first.last@[IPv6:::a3:a4:b1:ffff:11.22.33.44]", "first.last@[IPv6:::a2:a3:a4:b1:ffff:11.22.33.44]", "first.last@[IPv6:a1:a2:a3:a4::11.22.33.44]", "first.last@[IPv6:a1:a2:a3:a4:b1::11.22.33.44]", "first.last@[IPv6:a1::11.22.33.44]", "first.last@[IPv6:a1:a2::11.22.33.44]", "first.last@[IPv6:0123:4567:89ab:cdef::11.22.33.44]", "first.last@[IPv6:0123:4567:89ab:CDEF::11.22.33.44]", "first.last@[IPv6:a1::b2:11.22.33.44]", "[email protected]", "[email protected]", "[email protected]");// These should be invalid$arrayInValid = array("[email protected],com", "first\\@[email protected]", "123456789012345678901234567890123456789012345678901234567890@12345678901234567890123456789012345678901234 [...]", "first.last", "12345678901234567890123456789012345678901234567890123456789012345@iana.org", "[email protected]", "[email protected]", "[email protected]", "\"\\\"first\\\"last\\\"@iana.org\"", "\"\\\"\\\"\\\"@iana.org\"", "\"\\\"\\\\\"@iana.org\"", "\"\\\"\\\"@iana.org\"", "first\\@[email protected]", "first.last@", "x@x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23 [...]", "first.last@[.12.34.56.78]", "first.last@[12.34.56.789]", "first.last@[::12.34.56.78]", "first.last@[IPv5:::12.34.56.78]", "first.last@[IPv6:1111:2222:3333:4444:5555:12.34.56.78]", "first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:12.34.56.78]", "first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777]", "first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:8888:9999]", "first.last@[IPv6:1111:2222::3333::4444:5555:6666]", "first.last@[IPv6:1111:2222:333x::4444:5555]", "first.last@[IPv6:1111:2222:33333::4444:5555]", "[email protected]", "[email protected]", "first.last@x234567890123456789012345678901234567890123456789012345678901234.iana.org", "abc\\@[email protected]", "abc\\@iana.org", "\"Doug\\ \\\\\"Ace\\\\\"\\ [email protected]\"", "abc@[email protected]", "abc\\@[email protected]", "abc\\@iana.org", "@iana.org", "doug@", "\"\\\"[email protected]\"", "\"ote\\\"@iana.org\"", "[email protected]", "[email protected]", "[email protected]", "\"\\\"Doug \\\"Ace\\\" L.\\\"@iana.org\"", "\"Doug\\ \\\\\"Ace\\\\\"\\ L\\[email protected]\"", "hello [email protected]", "[email protected].", "test.iana.org", "[email protected]", "[email protected]", "[email protected]", "test@[email protected]", "test@@iana.org", "-- test [email protected]", "[test]@iana.org", "\"\\\"test\\\"test\\\"@iana.org\"", "()[]\\;:,><@iana.org", "test@.", "test@example.", "[email protected]", "test@1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 [...]", "test@[123.123.123.123", "[email protected]]", "NotAnEmail", "@NotAnEmail", "\"\\\"test\\\"blah\\\"@iana.org\"", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "Ima [email protected]", "phil.h\\@\\@[email protected]", "foo@[\\1.2.3.4]", "\"first.\\\"\\\"[email protected]\"", "first\\[email protected]", "Abc\\@[email protected]", "Fred\\ [email protected]", "Joe.\\[email protected]", "first.last@[IPv6:1111:2222:3333:4444:5555:6666:12.34.567.89]", "{^c\\@**Dog^}@cartoon.com", "\"\\\"foo\\\"(yay)@(hoopla)[1.2.3.4]\"", "cal(foo(bar)@iamcal.com", "cal(foo)bar)@iamcal.com", "cal(foo\\)@iamcal.com", "first(12345678901234567890123456789012345678901234567890)last@(123456789012345678901234567890123456789012 [...]", "first(middle)[email protected]", "\"first(abc(\\\"def\\\".ghi).mno)middle(abc(\\\"def\\\".ghi).mno).last@(abc(\\\"def\\\".ghi).mno)example(abc(\\\"def\\\".ghi).mno). [...]\"", "a(a(b(c)d(e(f))g)(h(i)j)@iana.org", ".@", "@bar.com", "@@bar.com", "aaa.com", "[email protected]", "[email protected]", "aaa@[123.123.123.123]a", "aaa@[123.123.123.333]", "[email protected].", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "\"\\\"Unicode NULL \\\"@char.com\"", "Unicode NULL @char.com", "first.last@[IPv6::]", "first.last@[IPv6::::]", "first.last@[IPv6::b4]", "first.last@[IPv6::::b4]", "first.last@[IPv6::b3:b4]", "first.last@[IPv6::::b3:b4]", "first.last@[IPv6:a1:::b4]", "first.last@[IPv6:a1:]", "first.last@[IPv6:a1:::]", "first.last@[IPv6:a1:a2:]", "first.last@[IPv6:a1:a2:::]", "first.last@[IPv6::11.22.33.44]", "first.last@[IPv6::::11.22.33.44]", "first.last@[IPv6:a1:11.22.33.44]", "first.last@[IPv6:a1:::11.22.33.44]", "first.last@[IPv6:a1:a2:::11.22.33.44]", "first.last@[IPv6:0123:4567:89ab:cdef::11.22.33.xx]", "first.last@[IPv6:0123:4567:89ab:CDEFF::11.22.33.44]", "first.last@[IPv6:a1::a4:b1::b4:11.22.33.44]", "first.last@[IPv6:a1::11.22.33]", "first.last@[IPv6:a1::11.22.33.44.55]", "first.last@[IPv6:a1::b211.22.33.44]", "first.last@[IPv6:a1::b2::11.22.33.44]", "first.last@[IPv6:a1::b3:]", "first.last@[IPv6::a2::b4]", "first.last@[IPv6:a1:a2:a3:a4:b1:b2:b3:]", "first.last@[IPv6::a2:a3:a4:b1:b2:b3:b4]", "first.last@[IPv6:a1:a2:a3:a4::b1:b2:b3:b4]"); $counertAll = 0;$counterValid = 0;foreach ($arrayValid as $emailTest) { $counertAll++; if(is_email($emailTest)) { $counterValid++; echo "$counterValid / $counertAll | $emailTest is a valid\n\r"; } else { "$counterValid / $counertAll | $emailTest is a INVALID\n\r"; }}foreach ($arrayInValid as $emailTest) { $counertAll++; if(is_email($emailTest)) { $counterValid++; echo "$counterValid / $counertAll | $emailTest is a valid\n\r"; } else { "$counterValid / $counertAll | $emailTest is a INVALID\n\r"; }}// Valid: 96/134// Invalid: 130/132?> Spoiler: Запустить Code: git clone https://github.com/dominicsayers/isemail cd ./isemail nano test_email.php # код сверху сюдым php -f test_email.php UPDATE: а бл.. ветка про питухон уже запостил пыху
у пыхи вообще из коробки можно https://www.php.net/manual/en/function.filter-var.php юзать с ключем FILTER_VALIDATE_EMAIL но там тоже есть нестыковка по RFC