S
S
Sergey Khlopov2020-02-24 15:29:28
Regular Expressions
Sergey Khlopov, 2020-02-24 15:29:28

Is it a good regular expression to search for email addresses?

Hello, please tell me, I made the following regular expression:

([a-zа-яё0-9._-]+)(@{1})([a-zа-яё0-9._-]+)\.([a-zа-я]{2,3})

The task builds the following:
The program takes an html file as input and you need to deliver all the email addresses from it, is this regular expression suitable for such a task or should something be improved? Thank you in advance for your response.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2020-02-24
@Shlop

In fact, seriously, it is impossible to create such a regexp. The problem is that there is no single rule for validating email addresses. Any [email protected] address can be an email address or something else. For example XMPP user (JID).

S
Saboteur, 2020-02-24
@saboteur_kiev

Here is an example of a more or less suitable regular expression for email:

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

But it does not cover all cases, so the correct answer is that you don’t need to write email address search/validator on regular expressions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question