tim_carter
asked on
How do i validate length of entire regex?
I am trying to build a regex to check an email address
/\w+([-+.']\w+)*@\w+([-.]\ w+)*\.\w+( [-.]\w+)*$ /
works fine, but i only want it to accept up to 256 chars.
something like
/\w+([-+.']\w+)*@\w+([-.]\ w+)*\.\w+( [-.]\w+)*$ {3,254}/
but ofcourse it does not work, how do i do this?
/\w+([-+.']\w+)*@\w+([-.]\
works fine, but i only want it to accept up to 256 chars.
something like
/\w+([-+.']\w+)*@\w+([-.]\
but ofcourse it does not work, how do i do this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you accept to code two different regexes, you can first perform a test on the length with
If the first expression matches, the address is too large.
/.{257}/
and then perform a text on the structure with your initial working regex
/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
If the first expression matches, the address is too large.
fred&barney@stonehenge.com
is a valid email address
is a valid email address
ASKER
/^(?=.{3,254}$)\w+([-+&.'] \w+)*@\w+( [-.]\w+)*\ .\w+([-.]\ w+)*$/
This regex is perfect
This regex is perfect
ASKER
Thanks.
/^(?=.{3,254}$)\w+([-+&.'] \w+)*@\w+( [-.]\w+)*\ .\w+([-.]\ w+)*$/
Modified with & because that is legal in local part
/^(?=.{3,254}$)\w+([-+&.']
Modified with & because that is legal in local part
This regex is perfectI am always amused when I see something like that in writing! The internet is littered with regular expressions that do not quite work right. Many of the worst offenders are regular expressions designed to tell if an email address is valid. That is probably the most important reason for development of this function:
http://php.net/manual/en/function.filter-var.php
Filter_var() has been available since the years-ago release of PHP 5.2. See:
http://php.net/manual/en/filter.filters.validate.php
PHP 5.2 is now obsolete and is not supported any more. PHP 5.2 is so old it is not even given security releases. So this is a good time to reconsider whether writing your own regex to validate emails still makes sense. It's kind of like adjusting a carburetor. It doesn't really matter how good you are with a carburetor. Carburetor adjustment is an obsolete skill. We don't have to do that any more because we have fuel injected engines. And we don't have to tinker with email validation because we have filter_var().
Filter_var() does not check whether the email is routable. I choose to include that test in the function posted at ID: 37749502 on line 56. If you're sending email to yourself at localhost, you might or might not want to include that functionality.
ASKER
Hi Ray,
I actually use filter_var in php to check email addresses.
Im am just practicing on regex. I do not main that it is perfect to validate an email, because there is still emails it will not validate.
I actually use filter_var in php to check email addresses.
Im am just practicing on regex. I do not main that it is perfect to validate an email, because there is still emails it will not validate.
Filter_var() is the right tool.
You might enjoy this article if you're learning about regular expressions.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
You might enjoy this article if you're learning about regular expressions.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
ASKER
Thanks ray, I will look at that article
Open in new window