-1

I am trying to wirte a regular expression for emails in JFlex. So far I tried with this

L=[a-zA-Z_]+
D=[0-9]+    
email=[^(.+)@(\S+)$]
%{
    public String lexeme;
%}
%%
{L}({L}|{D})* {lexeme=yytext(); return Identi;}
("(-"{D}+")")|{D}+ {lexeme=yytext(); return Number;}
{email} {lexeme=yytext(); return Email;}
 . {return ERROR;}

When I test with an email, the lexer is not matching any email. How to match email?

coding2
  • 47
  • 6

1 Answers1

0

I found the solution:

alphaNumeric=({L}+|{L}+{D}+)+
email={alphaNumeric}"@"{alphaNumeric}"."({L}+|{L}+"."{L}+)

This is the regular expression

coding2
  • 47
  • 6
  • This is a terrible regex for matching emails. It rejects *many* valid email addresses. – user229044 Jun 01 '22 at 18:40
  • And which is the best Regex for matching emails? – coding2 Jun 01 '22 at 18:42
  • The best pattern is the least restrictive one possible which still likely catches the wrong type of data (for example names, phone numbers or physical addresses):`.+@.+\..+`. See https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression – user229044 Jun 01 '22 at 18:43
  • Even without getting into [unicode/internationalization](https://en.wikipedia.org/wiki/International_email), you miss tons of very common emails, like `john.smith@gmail.com` or `john+smith@gmail.com` or `john@mail-server.com`. There are *way* more characters in emails that just the alphanumeric range: https://stackoverflow.com/a/2049510/229044 – user229044 Jun 01 '22 at 18:49