No need fora-zA-Z
, just use A-Z
and make the regex case insensitive with re.IGNORECASE
.
Also make sure you use
^
Assert position at the beginning of a line
and
$
Assert position at the end of a line
Python Example:
import re
match = re.search(r"^(?:mailto:)?([A-Z0-9_.+-]+@[A-Z0-9-]+\.[\tA-Z0-9-.]+)$", email, re.IGNORECASE)
if match:
result = match.group(1)
else:
result = ""
Demo:
https://regex101.com/r/cI1eD6/1
Regex explanation:
^(mailto:)?([A-Z0-9_.+-]+@[A-Z0-9-]+\.[A-Z0-9-.]+)$
Options: Case insensitive
Assert position at the beginning of a line «^»
Match the regex below and capture its match into backreference number 1 «(mailto:)?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the character string “mailto:” literally «mailto:»
Match the regex below and capture its match into backreference number 2 «([A-Z0-9_.+-]+@[A-Z0-9-]+\.[A-Z0-9-.]+)»
Match a single character present in the list below «[A-Z0-9_.+-]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
A character in the range between “A” and “Z” «A-Z»
A character in the range between “0” and “9” «0-9»
A single character from the list “_.+” «_.+»
The literal character “-” «-»
Match the character “@” literally «@»
Match a single character present in the list below «[A-Z0-9-]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
A character in the range between “A” and “Z” «A-Z»
A character in the range between “0” and “9” «0-9»
The literal character “-” «-»
Match the character “.” literally «\.»
Match a single character present in the list below «[A-Z0-9-.]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
A character in the range between “A” and “Z” «A-Z»
A character in the range between “0” and “9” «0-9»
A single character from the list “-.” «-.»
Assert position at the end of a line «$»