Matching Lua Email Template

I have the following code:

if not (email:match("[A-Za-z0-9%.]+@[%a%d]+%.[%a%d]+")) then
     print(false)
end

Currently it does not break

 "test@yahoo,ca"  or "test@test1.test2,com"

like a mistake.

I thought, restricting the input to the characters %aand %d- digits, by default I would catch any punctuation, including commas.

But I think I'm wrong. Or something else that I just don’t see. A second pair of eyes would be appreciated.

+4
source share
2 answers

In the example, the "test@test1.test2,com"pattern matches test@test1.test2and stops due to the following ,. He does not lie, he corresponds, just not what you expected. To fix, use the bindings:

^[A-Za-z0-9%.]+@[%a%d]+%.[%a%d]+$

You can simplify it even further:

^[%w.]+@%w+%.%w+$

which %wcorresponds to an alphanumeric character.

+4
source
+1

All Articles