Delphi Regular Expression Writing

Viewed 116

I am not very fluent in regular expressions with Delphi and am wondering if anyone can spot the issue in my current expression. I would like the expression to also include the + character when searching for valid emails. Can anyone give me a hand with this? TIA

Expression is this

TRegEx.Create('[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+', [roIgnoreCase, roSingleline])
1 Answers

You could add the + to the first 2 character classes. Note that you don't have to escape the . in the character class and you can move the - to the end.

[_a-zA-Z\d.+-]+@(?:[_a-zA-Z\d+-]+\.)+[_a-zA-Z\d-]+

Or perhaps a shorter variant, not matching a dot at the start and end.

[\w+]+(?:\.[\w+-])*@(?:[\w+-]+\.)+[a-zA-Z]{2,}
Related