// include the latest version of the regex crate in your Cargo.toml
extern crate regex;
use regex::Regex;
fn main() {
let regex = Regex::new(r"(?m)^(?<fullAddress>(?<mailbox>([a-zA-Z0-9\+\!\#\$\%\&\'\*\-\/\=\?\+\_\{\}\|\~]|(?<singleDot>(?<!\.)(?<!^)\.(?!\.))|(?<foldedWhiteSpace>\s?\&\#13\;\&\#10\;.)){1,64})\s?(?<singleAt>(?<!\@)(?<!\-)(?<!\.)\@(?!\@))(?<domain>(?<dns>[[:alnum:]]([[:alnum:]\-]{0,63}\.){1,24}[[:alnum:]\-]{1,63}[[:alnum:]])|(?<IPv4>\[((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\])|(?<IPv6Full>(\[IPv6(\:[0-9a-fA-F]{1,4}){8}\]))|(?<IPv6Comp1>\[IPv6\:((([0-9a-fA-F]{1,4})\:){1,3}(\:([0-9a-fA-F]{1,4})){1,5}?\])|\[IPv6\:((([0-9a-fA-F]{1,4})\:){1,5}(\:([0-9a-fA-F]{1,4})){1,3}?\]))|(?<IPv6Comp2>(\[IPv6\:\:(\:[0-9a-fA-F]{1,4}){1,6}\]))|(?<IPv6Comp3>(\[IPv6\:([0-9a-fA-F]{1,4}\:){1,6}\:\]))|(?<IPv6Comp4>(\[IPv6\:\:\:)\])|(?<IPv6v4Full>(\[IPv6(\:[0-9a-fA-F]{1,4}){6}\:((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3})(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\])|(?<IPv6v4Comp1>\[IPv6\:((([0-9a-fA-F]{1,4})\:){1,3}(\:([0-9a-fA-F]{1,4})){1,5}?(\:((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\])|\[IPv6\:((([0-9a-fA-F]{1,4})\:){1,5}(\:([0-9a-fA-F]{1,4})){1,3}?(\:((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\]))|(?<IPv6v4Comp2>(\[IPv6\:\:(\:[0-9a-fA-F]{1,4}){1,5}(\:((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\]))|(?<IPv6v4Comp3>(\[IPv6\:([0-9a-fA-F]{1,4}\:){1,5}\:(((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\]))|(?<IPv6v4Comp4>(\[IPv6\:\:\:((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3})(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\])))").unwrap();
let string = "Valid (as per futility) addresses that I agree are valid
========================================================
_somename@iana.org test62 Valid
_Yosemite.Sam@iana.org test128 Valid
!def!xyz%abc@iana.org test61 Valid
{_test_}@iana.org test90 Valid
+1~1+@iana.org test89 Valid
~@iana.org test129 Valid
$A12345@iana.org test60 Valid
1234567890@iana.org test85 Valid
1234567890123456789012345678901234567890123456789012345678@12345678901234567890123456789012345678901234567890123456789.12345678901234567890123456789012345678901234567890123456789.123456789012345678901234567890123456789012345678901234567890123.iana.org test9 Valid
1234567890123456789012345678901234567890123456789012345678901234@iana.org test2 Valid
a-b@bar.com test202 Valid
a@b.co-foo.uk test209 Valid
a@bar.com test193 Valid
aaa@[123.123.123.123] test197 Valid
cdburgess+!#$%&'*-/=?+_{}|~test@gmail.com test229 Valid
customer/department@iana.org test127 Valid
customer/department=shipping@iana.org test59 Valid
dclo@us.ibm.com test63 Valid
first.last @iana.org test223 Valid
first.last@[12.34.56.78] test10 Valid
first.last@[IPv6:::] test233 Valid
first.last@[IPv6:::1111:2222:3333:4444:5555:6666] test14 Valid
first.last@[IPv6:::12.34.56.78] test11 Valid
first.last@[IPv6:::a2:a3:a4:b1:ffff:11.22.33.44] test252 Valid
first.last@[IPv6:::a3:a4:b1:ffff:11.22.33.44] test251 Valid
first.last@[IPv6:1111:2222:3333::4444:5555:6666:7777] test45 Valid
first.last@[IPv6:::b3:b4] test239 Valid
first.last@[IPv6:::b4] test236 Valid
first.last@[IPv6:0123:4567:89ab:cdef::] test249 Valid
first.last@[IPv6:0123:4567:89ab:CDEF::] test250 Valid
first.last@[IPv6:0123:4567:89ab:cdef::11.22.33.44] test262 Valid
first.last@[IPv6:0123:4567:89ab:CDEF::11.22.33.44] test264 Valid
first.last@[IPv6:1111:2222:3333::4444:12.34.56.78] test12 Valid
first.last@[IPv6:1111:2222:3333::4444:5555:12.34.56.78] test38 Valid
first.last@[IPv6:1111:2222:3333::4444:5555:6666] test15 Valid
first.last@[IPv6:1111:2222:3333:4444:5555:6666::] test16 Valid
first.last@[IPv6:1111:2222:3333:4444:5555:6666:12.34.56.78] test13 Valid
first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:8888] test17 Valid
first.last@[IPv6:a1::] test244 Valid
first.last@[IPv6:a1::11.22.33.44] test258 Valid
first.last@[IPv6:a1::b2:11.22.33.44] test270 Valid
first.last@[IPv6:a1::b4] test241 Valid
first.last@[IPv6:a1:a2::] test247 Valid
first.last@[IPv6:a1:a2::11.22.33.44] test260 Valid
first.last@[IPv6:a1:a2:a3:a4::11.22.33.44] test253 Valid
first.last@[IPv6:a1:a2:a3:a4:b1::11.22.33.44] test254 Valid
first.last@123.iana.org test20 Valid
first.last@3com.com test19 Valid
first.last@example.123 test48 Valid
first.last@iana.org test1 Valid
first.last@x23456789012345678901234567890123456789012345678901234567890123.iana.org test18 Valid
foobar@192.168.0.1 test216 Valid
Ima.Fool@iana.org test135 Valid
name.lastname@domain.com test188 Valid
peter.piper@iana.org test66 Valid
shaitan@my-domain.thisisminekthx test214 Valid
t*est@iana.org test88 Valid
test-test@iana.org test87 Valid
test. obs@syntax.com test224 Valid
Test. Folding. Whitespace@iana.org test152 Valid
test.test@iana.org test92 Valid
test@[123.123.123.123] test98 Valid
test@123.123.123.123 test97 Valid
test@123.123.123.x123 test96 Valid
test@example.example.iana.org test100 Valid
test@example.iana.org test99 Valid
test@iana.org test83 Valid
TEST@iana.org test84 Valid
test@test.com test277 Valid
test@xn--example.com test279 Valid
test+test@iana.org test86 Valid
user%uucp!path@berkeley.edu test220 Valid
user+mailbox@iana.org test58 Valid
valid@about.museum test212 Valid
Valid (as per futility) addresses that are invalid as per RFC5321
============================================================================
+@b.c test203 Valid
first.last@com test49 Valid
a@b test190 Valid
a@bar test201 Valid
test@Bücher.ch test280 Valid
test@example test116 Valid
first.last@[IPv6:::a2:a3:a4:b1:b2:b3:b4] test230 Valid
first.last@[IPv6:a1:a2:a3:a4:b1:b2:b3::] test231 Valid
+@b.com test204 Valid
My tests for valid IPv6 and v6v4 addresses
==========================================
first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:8888]
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::8888]
first.last@[IPv6:1111:2222:3333:4444::7777:8888]
first.last@[IPv6:1111:2222:3333::6666:7777:8888]
first.last@[IPv6:1111:2222::5555:6666:7777:8888]
first.last@[IPv6:1111::4444:5555:6666:7777:8888]
first.last@[IPv6:::3333:4444:5555:6666:7777:8888]
first.last@[IPv6:1111:2222:3333:4444:5555::]
first.last@[IPv6:1111:2222:3333:4444::8888]
first.last@[IPv6:1111:2222:3333::7777:8888]
first.last@[IPv6:1111:2222::6666:7777:8888]
first.last@[IPv6:1111::5555:6666:7777:8888]
first.last@[IPv6:::4444:5555:6666:7777:8888]
first.last@[IPv6:::3333:4444:5555:6666:7777:8888]
first.last@[IPv6:1111:2222:3333:4444::12.34.56.78]
first.last@[IPv6:1111:2222:3333::6666:12.34.56.78]
first.last@[IPv6:1111:2222::5555:6666:12.34.56.78]
first.last@[IPv6:1111::4444:5555:6666:12.34.56.78]
first.last@[IPv6:::3333:4444:5555:6666:12.34.56.78]
Invalid (as per futility) addresses with valid mailbox defective domain
=======================================================================
x@x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x2 test8 Valid
123456789012345678901234567890123456789012345678901234567890@12345678901234567890123456789012345678901234567890123456789.12345678901234567890123456789012345678901234567890123456789.12345678901234567890123456789012345678901234567890123456789.12345.iana.org test21 Invalid
first.last@[IPv6:1111:2222:33333::7777:8888] test47 Invalid
aaa@[123.123.123.123]a test198 Invalid
a@b-.com test206 Invalid
a@bar.com. test200 Invalid
a@-b.com test205 Invalid
aaa@.123 test196 Invalid
aaa@.com test195 Invalid
aaa@[123.123.123.333] test199 Invalid
abc@def@iana.org test69 Invalid
doug@ test73 Invalid
first.last@ test33 Invalid
first.last@-xample.com test50 Invalid
first.last@[::12.34.56.78] test37 Invalid
first.last@[.12.34.56.78] test35 Invalid
first.last@[12.34.56.789] test36 Invalid
first.last@[IPv5:::12.34.56.78] test37 Invalid
first.last@[IPv6::::] test234 Invalid
first.last@[IPv6::::11.22.33.44] test256 Invalid
first.last@[IPv6::::b3:b4] test240 Invalid
first.last@[IPv6::::b4] test237 Invalid
first.last@[IPv6::] test232 Invalid
first.last@[IPv6::11.22.33.44] test255 Invalid
first.last@[IPv6::a2::b4] test273 Invalid
first.last@[IPv6::a2:a3:a4:b1:b2:b3:b4] test275 Invalid
first.last@[IPv6::b3:b4] test238 Invalid
first.last@[IPv6::b4] test235 Invalid
first.last@[IPv6:0123:4567:89ab:cdef::11.22.33.xx] test263 Invalid
first.last@[IPv6:0123:4567:89ab:CDEFF::11.22.33.44] test265 Invalid
first.last@[IPv6:1111:2222::3333::4444:5555:6666] test44 Invalid
first.last@[IPv6:1111:2222:3333:4444:5555:12.34.56.78] test40 Invalid
first.last@[IPv6:1111:2222:3333:4444:5555:6666:12.34.567.89] test158 Invalid
first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:12.34.56.78] test41 Invalid
first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:8888:9999] test43 Invalid
first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777] test42 Invalid
first.last@[IPv6:1111:2222:333x::4444:5555] test46 Invalid
first.last@[IPv6:a1:::] test245 Invalid
first.last@[IPv6:a1:::11.22.33.44] test259 Invalid
first.last@[IPv6:a1:::b4] test242 Invalid
first.last@[IPv6:a1::11.22.33.44.55] test268 Invalid
first.last@[IPv6:a1::11.22.33] test267 Invalid
first.last@[IPv6:a1::a4:b1::b4:11.22.33.44] test266 Invalid
first.last@[IPv6:a1::b2::11.22.33.44] test271 Invalid
first.last@[IPv6:a1::b211.22.33.44] test269 Invalid
first.last@[IPv6:a1::b3:] test272 Invalid
first.last@[IPv6:a1:] test243 Invalid
first.last@[IPv6:a1:11.22.33.44] test257 Invalid
first.last@[IPv6:a1:a2:::] test248 Invalid
first.last@[IPv6:a1:a2:::11.22.33.44] test261 Invalid
first.last@[IPv6:a1:a2:] test246 Invalid
first.last@[IPv6:a1:a2:a3:a4::b1:b2:b3:b4] test276 Invalid
first.last@[IPv6:a1:a2:a3:a4:b1:b2:b3:] test274 Invalid
first.last@exampl-.com test51 Invalid
first.last@sub.do,com test3 Invalid
first.last@x234567890123456789012345678901234567890123456789012345678901234.iana.org test52 Invalid
foo@[\\1.2.3.4] test149 Invalid
gatsby@f.sc.ot.t.f.i.tzg.era.l.d. test82 Invalid
Invalid@about.museum- test213 Invalid
test@[123.123.123.123 test117 Invalid
test@@iana.org test106 Invalid
test@123.123.123.123] test118 Invalid
test@123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012.com test115 Invalid
test@example. test113 Invalid
test@example.com test278 Invalid
x@x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456 test34 Invalid
Invalid (as per futility) addresses with valid domain defective mailbox
=======================================================================
-- test --@iana.org test107 Invalid
.@iana.org test133 Invalid
.dot@iana.org test76 Invalid
.first.last@iana.org test24 Invalid
.test@iana.org test104 Invalid
.wooly@iana.org test130 Invalid
()[]\\;:,><@iana.org test111 Invalid
[test]@iana.org test108 Invalid
{^c\\@**Dog^}@cartoon.com test161 Invalid
@iana.org test72 Invalid
12345678901234567890123456789012345678901234567890123456789012345@iana.org test23 Invalid
Abc\\@def@iana.org test155 Invalid
abc\\@def@iana.org test64 Invalid
abc\\@iana.org test71 Invalid
abc\\\\@def@iana.org test70 Invalid
abc\\\\@iana.org test65 Invalid
dot.@iana.org test77 Invalid
first..last@iana.org test26 Invalid
first.last.@iana.org test25 Invalid
first(12345678901234567890123456789012345678901234567890)last@(1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890)iana.org test173 Invalid
first(abc(\\\"def\\\".ghi).mno)middle(abc(\\\"def\\\".ghi).mno).last@(abc(\\\"def\\\".ghi).mno)example(abc(\\\"def\\\".ghi).mno).(abc(\\\"def\\\".ghi).mno)com(abc(\\\"def\\\".ghi).mno) test183 Invalid
first(middle)last@iana.org test179 Invalid
first\\@last@iana.org test5 Invalid
first\\\\@last@iana.org test32 Invalid
first\\last@iana.org test154 Invalid
Fred\\ Bloggs@iana.org test156 Invalid
hello world@iana.org test81 Invalid
Ima Fool@iana.org test138 Invalid
Invalid \\ Folding \\ Whitespace@iana.org test218 Invalid
Joe.\\\\Blow@iana.org test157 Invalid
NotAnEmail test119 Invalid
ote\\\"@iana.org test75 Invalid
phil.h\\@\\@ck@haacked.com test139 Invalid
pootietang.@iana.org test132 Invalid
test..test@iana.org test103 Invalid
test.@iana.org test102 Invalid
test. obs@syntax.com test225 Invalid
test.iana.org test101 Invalid
test@. test112 Invalid
test@...........com test215 Invalid
test@.org test114 Invalid
test@test@iana.org test105 Invalid
two..dot@iana.org test78 Invalid
Unicode NULL \\\\\\u0000@char.com test228 Invalid
wo..oly@iana.org test131 Invalid
Invalid (as per futility) addresses with defective mailbox and domain
=======================================================================
-- test --@iana.org test107 Invalid
-@..com test207 Invalid
-@a..com test208 Invalid
.@ test189 Invalid
@@bar.com test192 Invalid
@bar.com test191 Invalid
@NotAnEmail test120 Invalid
aaa.com test194 Invalid
first.last test22 Invalid
Valid (as per futility) addresses that use parenthesis or quotes
================================================================
( x ) first ( x ) . ( x) last ( x ) @iana.org test222 Valid
1234 @ local(blah) .machine .example test178 Valid
a(a(b(c)d(e(f))g)h(i)j)@iana.org test186 Valid
c@(Chris's host.)public.example test176 Valid
cal(foo\\)bar)@iamcal.com test167 Valid
cal(foo\\@bar)@iamcal.com test166 Valid
cal(woo(yay)hoopla)@iamcal.com test165 Valid
cal@iamcal(woo).(yay)com test163 Valid
first.last@x(1234567890123456789012345678901234567890123456789012345678901234567890).com test185 Valid
HM2Kinsists@(that comments are allowed)this.is.ok test219 Valid
jdoe@machine(comment). example test177 Valid
first.(\\\")middle.last(\\\")@iana.org test182 Valid
first.( middle )last@iana.org test172 Valid
first.\\\"last\\\"@iana.org test144 Valid
first.\\\"mid\\\\dle\\\".\\\"last\\\"@iana.org test151 Valid
first(Welcome to the (\\\"wonderful\\\" (!)) world of email)@iana.org test174 Valid
pete(his account)@silly.test(his host) test175 Valid
first().last@iana.org test171 Valid
first(a\\\"bc.def).last@iana.org test181 Valid
first(abc.def).last@iana.org test180 Valid
first(abc\\(def)@iana.org test184 Valid
first().last@iana.org test171 Valid
first(a\\\"bc.def).last@iana.org test181 Valid
first(abc.def).last@iana.org test180 Valid
first(abc\\(def)@iana.org test184 Valid
(foo)cal(bar)@(baz)iamcal.com(quux) test162 Valid
test.\\\"test\\\"@iana.org test94 Valid
\"\\\"Unicode NULL \\\\\\u0000\\\"@char.com test226 Valid
\\\"[[ test ]]\\\"@iana.org test91 Valid
\\\"Abc@def\\\"@iana.org test56 Valid
\\\"Abc\\\\@def\\\"@iana.org test53 Valid
\\\"Austin@Powers\\\"@iana.org test134 Valid
\\\"Doug \\\\\\\"Ace\\\\\\\" L.\\\"@iana.org test68 Valid
\\\"first..last\\\"@iana.org test148 Valid
\\\"first.middle.last\\\"@iana.org test147 Valid
\\\"first.middle\\\".\\\"last\\\"@iana.org test146 Valid
\\\"first(last)\\\"@iana.org test221 Valid
\\\"first@last\\\"@iana.org test6 Valid
\\\"first\\\".\\\"last\\\"@iana.org test140 Valid
\\\"first\\\".\\\"middle\\\".\\\"last\\\"@iana.org test145 Valid
\\\"first\\\".last@iana.org test143 Valid
\\\"first\\\".middle.\\\"last\\\"@iana.org test141 Valid
\\\"first\\\\\\\"last\\\"@iana.org test4 Valid
\\\"first\\\\\\\\\\\\\\\"last\\\"@iana.org test150 Valid
\\\"first\\\\\\\\last\\\"@iana.org test7 Valid
\\\"first\\\\last\\\"@iana.org test28 Valid
\\\"Fred Bloggs\\\"@iana.org test57 Valid
\\\"Fred\\\\ Bloggs\\\"@iana.org test54 Valid
\\\"hello my name is\\\"@stutter.com test210 Valid
\\\"Ima Fool\\\"@iana.org test137 Valid
\\\"Ima.Fool\\\"@iana.org test136 Valid
\\\"Joe.\\\\\\\\Blow\\\"@iana.org test55 Valid
\\\"Joe\\\\\\\\Blow\\\"@iana.org test217 Valid
\\\"Test \\\\\\\"Fail\\\\\\\" Ing\\\"@iana.org test211 Valid
\\\"test.test\\\"@iana.org test93 Valid
\\\"test@test\\\"@iana.org test95 Valid
\\\"test\\\\\\\"blah\\\"@iana.org test125 Valid
\\\"test\\\\\\\\blah\\\"@iana.org test121 Valid
\\\"test\\\\ blah\\\"@iana.org test123 Valid
\\\"test\\\\blah\\\"@iana.org test122 Valid
\\\"test\\\\test\\\"@iana.org test109 Valid
\\\"test blah\\\"@iana.org test160 Valid
Invalid (as per futility) addresses that use parenthesis or quotes
==================================================================
\\\"\\\"@iana.org test31 Invalid
\\\"\\\"\\\"@iana.org test29 Invalid
\\\"\\\\\\\"@iana.org test30 Invalid
\\\"Doug \\\"Ace\\\" L.\\\"@iana.org test79 Invalid
\\\"first\\\"last\\\"@iana.org test27 Invalid
\\\"first\\\\\\\\\\\"last\\\"@iana.org test142 Invalid
\\\"foo\\\"(yay)@(hoopla)[1.2.3.4] test164 Invalid
\\\"qu@iana.org test74 Invalid
\\\"test\\\"blah\\\"@iana.org test126 Invalid
\\\"test\\\"test\\\"@iana.org test110 Invalid
\\\"test\\ blah\\\"@iana.org test159 Invalid
\\\"test blah\\\"@iana.org test124 Invalid
\\\"Unicode NULL \\u0000\\\"@char.com test227 Invalid
a(a(b(c)d(e(f))g)(h(i)j)@iana.org test187 Invalid
cal(foo(bar)@iamcal.com test168 Invalid
cal(foo)bar)@iamcal.com test196 Invalid
cal(foo\\)@iamcal.com test170 Invalid
Doug\\\\ \\\\\\\"Ace\\\\\\\"\\\\ L\\\\.@iana.org test80 Invalid
Doug\\\\ \\\\\\\"Ace\\\\\\\"\\\\ Lovell@iana.org test67 Invalid
first.\\\"\\\".last@iana.org test153 Invalid";
// result will be an iterator over tuples containing the start and end indices for each match in the string
let result = regex.captures_iter(string);
for mat in result {
println!("{:?}", mat);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Rust, please visit: https://docs.rs/regex/latest/regex/