简体   繁体   中英

PHP Regex for checking A-Z a-z 0-9 _ and

what I need is not email validation.. Its simple. Allow @hello.world or @hello_world or @helloworld but @helloworld. should be taken as @helloworld so as @helloworld?

In short check for alphabet or number after . and _ if not than take the string before it.

My existing RegEx is /@.([A-Za-z0-9_]+)(?=\\?|\\,|\\;|\\s|\\Z)/ it only cares with @helloworld and not the @hello.world or @hello_world.

Update:

So now I got a regex which deals with problem number 1. ie Allow @hello.world or @hello_world or @helloworld but still What about @helloworld. should be taken as @helloworld so as @helloworld?

New RegEx: /@([A-Za-z0-9+_.-]+)/

Don't use a regex for that.

Use...

$valid = filter_var($str, FILTER_VALIDATE_EMAIL);

Regex will never be able to verify an email, only to do some very basic format checking.

The most comprehensive regex for matching email addresses was 8000 chars long, and that one is already invalid due to changes in what is accepted in emails.

Use some designed library for the checking if you need to get real verification, otherwise just check for @ and some dots, anything more and you will probably end up invalidating perfectly legal email addresses.

Some examples of perfectly legal email addresses: (leading and trailing " are for showing boundary only"

"dama@nodomain.se"
"\"dama\"@nodomain.se"
"da/ma@nodomain.se"
"dama@nõdomain.se"
"da.ma@nodomain.se"
"dama@pa??de??µa.d???µ?"
"dama @nodomain .se"
"dama@nodomain.se "

You can use this regexp to validate email addresses

^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[AZ]{2,6}$.

For more information and complete complete expressions you can check here

I hope this helps you

尝试这个:

\@.+(\.|\?|;|[\r\n\s]+)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM