#10004: in Q encoded word ignore '=xx' when xx is not valid hex.

Bug report and fix by Thomas Guettler.
diff --git a/Lib/email/quoprimime.py b/Lib/email/quoprimime.py
index 12df028..168dfff 100644
--- a/Lib/email/quoprimime.py
+++ b/Lib/email/quoprimime.py
@@ -294,4 +294,4 @@
     the high level email.header class for that functionality.
     """
     s = s.replace('_', ' ')
-    return re.sub(r'=\w{2}', _unquote_match, s, re.ASCII)
+    return re.sub(r'=[a-fA-F0-9]{2}', _unquote_match, s, re.ASCII)
diff --git a/Lib/email/test/test_email.py b/Lib/email/test/test_email.py
index e18ffaf..b6e4831 100644
--- a/Lib/email/test/test_email.py
+++ b/Lib/email/test/test_email.py
@@ -1659,6 +1659,12 @@
             dh = decode_header(s % q)
             self.assertEqual(dh, [(a, 'iso-8859-1')])
 
+    def test_rfc2047_Q_invalid_digits(self):
+        # issue 10004.
+        s = '=?iso-8659-1?Q?andr=e9=zz?='
+        self.assertEqual(decode_header(s),
+                        [(b'andr\xe9=zz', 'iso-8659-1')])
+
 
 # Test the MIMEMessage class
 class TestMIMEMessage(TestEmailBase):