In: johnson@email.com...
I need to make sure that 'johnson' is valid by server/client standards (probably RFC 5322) for the username part of an email address. That is, Gmail and Thunderbird would accept them.
This question addresses full email addresses, which I don't need: How can I validate an email address using a regular expression?
This unpopular question is about JavaScript and doesn't have answers: Validating username part of email address
This answer to the afirst question above offers a semi-acceptable regex for a full email address, which I don't need, though it seems there might be room for improvement, but I might not need improvement: https://stackoverflow.com/a/201378/10343144
My current best solution would be to take:
emailusername="$1"
testemail="${emailusername}@nonsense.com"
regex='some existing full-email regex'
if [[ "${testemail}" =~ ${regex} ]]; then
echo "it works"
fi
But, that doesn't address the email username part specifically, and it's more expensive than validating only the username part.
Is adding a nonsense domain to the username for a regex check the best way?
Or is there a regex that can handle only only the username part of an email address?