A
A
Anton2016-01-06 18:27:03
C++ / C#
Anton, 2016-01-06 18:27:03

Where is the error in the regular expression?

Hello!
This code returns false:

if (regex_match("[email protected]", regex("/^[a-zA-Z0-9._-][email protected][a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$/")))
{
  return true;
}
else
{
  return false;
}

Where did you mess up?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Stalker_RED, 2016-01-06
@hummingbird

UPD: In fact, it's all about the extra slashes " / regexp_here / "
Instead \\., use \.
https://regex101.com/r/kY0uS0/2
But in general, this is not serious. This thing will not miss a bunch of valid addresses.
Examples of regular expressions are
more impressive www.regular-expressions.info/email.html
stackoverflow.com/questions/46155/validate-email-a...
Here is a "training" regular expression for you: \[email protected]\S+
In fact, only the presence of a dog and "some" characters is checked before and after her.
https://regex101.com/r/cG7dW0/1

A
AtomKrieg, 2016-01-06
@AtomKrieg

if (regex_match("[email protected]", regex("^[a-zA-Z0-9._-][email protected][a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$")))
    cout << "match" << endl;
  else
    cout << "no match" << endl;

A
abcd0x00, 2016-01-06
@abcd0x00

Where did you mess up?

Skip
[email protected]

A
Alexander Latyshev, 2016-01-06
@magalex

You should replace it with \\.AND \.
as a general recommendation: for simplicity, you should use special characters, for example "/^[\w.-][email protected][\w.-]+\\.[a-z]{2,4}$/i", where \wis equivalent to [a-zA-Z0-9_], and the "i" flag indicates that you should ignore the case of characters when searching for matches.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question