regular expression to match emails - php

i am stuck at particular problem i have username field where on only alphabets numbers and . - and _ are allowed and should always start with alphabet
here are examples what are accepted
someone#mydomain.com
something1234#mydomain.com
someething.something#mydomain.com
something-something#mydomain.com
something_something#mydomain.com
something_1234#mydomain.com
something.123#mydomain.com
something-456#mydomain.com
what i have done till now is
[a-zA-Z0-9]+[._-]{1,1}[a-zA-Z0-9]+#mydomain.com
this matches all my requirement except of problem it dosent match
someone#mydomain.com
someont123#mydomain.com
but it even matches
someone_someone_something#mydomain.com
which is not required i am really not getting how to solve this one thing i tried is
[a-zA-Z0-9]+[._-]{0}[a-zA-Z0-9]+#mydomain.com
but this is also not solving my problem now it accepts everything like
something+455#mydomain.com
which is not required please help me

If you want to make the - or . optional, then you have to replace the {1,1} (quantifier: once) with an ? (quantifier: one or none) here:
[a-zA-Z0-9]+[._-]?[a-zA-Z0-9]+#mydomain.com
The reason this regex also matches shorter addresses without delimiter -._ is that you don't assert the whole string, but just some part of it. Use start ^ and end $ anchors:
^[a-zA-Z0-9]+[._-]?[a-zA-Z0-9]+#mydomain\.com$

This is why we have filter_var($email, FILTER_VALIDATE_EMAIL).
If email address is valid, then you just have to check if it ends with #domain.com. That could be done with strrpos($email, '#domain.com').

Related

Regex with double negative matches

Given a series of strings:
error.user
success
success.user
success.admin
I want to write a regex that will match anything not starting with error, and that also doesn't have .user in it. So for this list, success and success.admin
What I've got so far is: /^((?!error)\w*)((?!\.*user)\w*)/
The first part: ((?!error)\w*) is working fine, and narrowing down the matches to just strings that start with success. For some reason the second part: ((?!\.*user)\w*) is doing precisely nothing. I think the first part is matching too much.
I'm doing this in PHP/PCRE
Here's my regex101.com link: https://regex101.com/r/l2sZru/1
You need to fix your negative regex like this:
^(?!error|.*\.user)[\w.]+$
RegEx Demo
Here (?!error|.*\.user) will assert failure if error is at the start OR if .user` is found anywhere in the input.
(?!\.*user) in your regex means assert failure when input has 0 or more DOTs followed by user at the start only.

what is the proper way of using an if condition with RegEx

I have been trying to validate a form where the input is the first and last name using regex in PHP. All I need the regex to do is check to make sure that there are no numbers. This is what I have right now:
if (preg_match('/\A\b[^0-9]*\W[^0-9]*\b\Z/sm', $name)) {
# Successful match
$nameError = "";
echo $name;
} else {
# Match attempt failed\
$nameError = "No Numbers";
}
The $name variable holds First and last name. I have been trying to make this work and I have not been able to get the input to match the regex. Am I using this correctly or do I need to input it in another way. Thank you for your help
if name is surename and first name you should use condition depending on country for example in Poland it would be
preg_match('/[a-z]+ [a-z]+/i',$name);
It means that all the names that contains two part that are alphabetic with space separating them are good. If you want first letter of name to be upper you should change it to
preg_match('/[A-Z][a-z]+ [A-Z][a-z]+/',$name);
Preg_match returns true if $name is validated by regular expression that you provide in the first argument.
So your usage of this function is okay, you should check your expression.
http://pl1.php.net/preg_match
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
You can always check your regex on online checker for example
http://www.solmetra.com/scripts/regex/
If you just want two words separated by one space, this will do what you want: if (preg_match('/^[A-Za-z]+ [A-Za-z]+$/', $name))
Thank you all for your replies, I found the answer in the most obvious place though and it didn't have anything to do with the regex. I forgot to setup the variables correctly for using them on the same page as the form. Stupid mistake. Anyway, thank you again.

Beginner regexp case, simple match

I want to create a pregmatch pattern which applies to:
http://site.local/app/**/admin
text. I created something, which looks good, but it also pass the
http://site.local/app/vf/adming
what I dont want to. The basically created pattern:
preg_match('/http:\/\/site.local\/app\/.*\\/admin/', $siteUrl)
how should it be corrected?
Btw: operators/admins, I created this thread previously and since then that account is disabled. https://stackoverflow.com/questions/11139579/i-need-a-regexp Now that you see, I really tried it hard, may I get that account back? If not, I understand
[a-zA-Z] only letters and {1,5} from 1 to 5 length. If you to allow numbers just change it to [a-zA-Z0-9]
$site = 'http://site.local/app/at/admin';
if(preg_match('/^http:\/\/site.local\/app\/[a-zA-Z]{1,5}\/admin$/', $site)){
echo 1;
}
Use ^ and $ to "tell" regex the start and end of your pattern.
preg_match('/^http:\/\/site.local\/app\/(.*)\/admin$/', 'http://site.local/app/abcd/admin');
preg_match('/^http:\/\/site.local\/app\/(.*)\/admin$/', 'http://site.local/app/abcd/admins');
I would say the problem is that .* matches all characters where you actually want to match two *.
/^http:\/\/site.local\/app\/[\*]{2}\\/admin$/
Should do it...
Edit: To exlpain myself to the person who marked down.
The asker said he wanted a preg_match to match the
text
http://site.local/app/**/admin
I did just that. How can you mark me down for understanding English?
But to statisfy the asker cos he did mean any chars and any number of chars between app and admin here is the amended version:
/^http:\/\/site.local\/app\/.*\\/admin$/

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. i.e. 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.-]+.[A-Z]{2,6}$.
For more information and complete complete expressions you can check here
I hope this helps you
Try this:
\#.+(\.|\?|;|[\r\n\s]+)

Regular expression for e-mail domain (not basic e-mail verification)

I'm currently using
if(preg_match('~#(semo\.edu|uni\.uu\.se|)$~', $email))
as a domain check.
However I need to only check if the e-mail ends with the domains above. So for instance, all these need to be accepted:
hello#semo.edu
hello#student.semo.edu
hello#cool.teachers.semo.edu
So I'm guessing I need something after the # but before the ( which is something like "any random string or empty string". Any regexp-ninjas out there who can help me?
([^#]*\.)? works if you already know you're dealing with a valid email address. Explanation: it's either empty, or anything that ends with a period but does not contain an ampersand. So student.cs.semo.edu matches, as does plain semo.edu, but not me#notreallysemo.edu. So:
~#([^#]*\.)?(semo\.edu|uni\.uu\.se)$~
Note that I've removed the last | from your original regex.
You can use [a-zA-Z0-9\.]* to match none or more characters (letters, numbers or dot):
~#[a-zA-Z0-9\.]*(semo\.edu|uni\.uu\.se|)$~
Well .* will match anything. But you don't actually want that. There are a number of characters that are invalid in a domain name (ex. a space). Instead you want something more like this:
[\w.]*
I might not have all of the allowed characters, but that will get you [A-Za-z0-9_.]. The idea is that you make a list of all the allowed characters in the square brakets and then use * to say none or more of them.

Categories