Instagram username Regex -PHP - php

I have register form and input of "instagram-username".
Instagram username can included only: a-z A-Z 0-9 dot(.) underline(_)
This is my code:
if(!empty($instaUsername) && preg_match("/([A-Za-z._])\w+/", $instaUsername)) {
throw new Exception ("Your name Instagram is incorrect");
}
When $instaUsername = "name.123" or "name_123" this give me the error.
How to make a regular expression according to the following requirements?
a-z A-Z 0-9 dot(.) underline(_)
I would love to have good tutorials on regex as comment.

jstassen has a good write up on insta user names:
(?:^|[^\w])(?:#)([A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:\.(?!\.))){0,28}(?:[A-Za-z0-9_]))?)

Thus you want a regex what validates Instagram usernames? Well then, shall we first do some research on what the requirements actually are? Okay let's start!
...
Well it seems like Instagram doesn't really speak out about the requirements of a valid username. So I made an account and checked what usernames it accepts and what usernames are rejected to get a sense of the requirements. The requirements I found are as following:
The length must be between 3 and 30 characters.
The accepted characters are like you said: a-z A-Z 0-9 dot(.) underline(_).
It's not allowed to have two or more consecutive dots in a row.
It's not allowed to start or end the username with a dot.
Putting all of that together will result in the following regex:
^[\w](?!.*?\.{2})[\w.]{1,28}[\w]$
Try it out here with examples!

Moving forward from the comment section, this is what you want:
if(!empty($instaUsername) && preg_match('/^[a-zA-Z0-9._]+$/', $instaUsername)) {
throw new Exception ("Your name Instagram is incorrect");
}

This regex should work for you:
~^([a-z0-9._])+$~i
You can use anchors to match the start (^) and the end $ of your input. And use the modifier i for case-insensitivity.

I found a lot of the answers here way too complicated and didn't account for a username being used in a sentence (like hey #man... what's up?), so I did my own
/#(?:[\w][\.]{0,1})*[\w]/
This says:
#: Find an # symbol (optional)
([\w][\.]{0,1})+: Find a word character (A-Z, 0-9, _) and up to 1 dot in a row, * as many times as you like (e.g. allow #u.s.e.r.n.a.m.e, but not #u..sername)
(?: this before the above just means "don't actually create a capturing group for this"
[\w] end with a word character (i.e. don't allow a final dot, like #username. should exclude the dot)
If you want to limit the username to 30chars like IG does, then:
#(?:(?:[\w][\.]{0,1})*[\w]){1,29}
This just wraps everything in another non-capturing (?: group and limits the total length to 29 chars

Okay so my edit was rejected because i should have posted it as an answer or comment so
if(!empty($instaUsername) && !preg_match("/([A-Za-z._])\w+/", $instaUsername)) {
throw new Exception ("Your name Instagram is incorrect");
}

i use this pattern
if(empty($instaUsername) or preg_match("/[a-z|A-Z|0-9|\.|\_]+$/", $instaUsername)==false) {
throw new Exception ("Your name Instagram is incorrect");
}

Reg
Useful regex
(?!.*\.\.)(?!.*\.$)[^\W][\w.]{0,29}$

Related

Unique regex for name validation

I want to check is the name valid with regex PHP, but i need a unique regex that allows:
Letters (upper and lowercase)
Spaces (max 2)
But there can't be a space after space..
For example:
Name -> Dennis Unge Shishic (valid)
Name -> Denis(space)(space) (not valid)
Hope you guys understand me, thank you :)
First, it's worth mentioning that having such restrictive rules for the names of persons is a very bad idea. However, if you must, a simple character class like this will limit you to just uppercase and lowercase English letters:
[A-Za-z]
To match one or more, you need to add a + after it. So, this will match the first part of the name:
[A-Za-z]+
To capture a second name, you just need to do the same thing preceded by a space, so something like this will capture two names:
[A-Za-z]+ [A-Za-z]+
To make the second name optional, you need to surround it by parentheses and add a ? after it, like this:
[A-Za-z]+( [A-Za-z]+)?
And to add a third name, you just need to do it again:
[A-Za-z]+( [A-Za-z]+)? [A-Za-z]+
Or, you could specify that the latter names can repeat between 1 and 2 times, like this:
[A-Za-z]+( [A-Za-z]+){1,2}
To make the resulting code easy to understand and maintain, you could use two Regex. One checking (by requiring it to be true) that only the allowed characters are used ^[a-zA-Z ]+$ and then another one, checking (by requiring it to be false) that there are no two (or more) adjacent spaces ( ){2,}
Try following working code:
Change input to whatever you want to test and see correct validation result printed
<?php
$input_line = "Abhishek Gupta";
preg_match("/[a-zA-Z ]+/", $input_line, $nameMatch);
preg_match("/\s{2,}/", $input_line, $multiSpace);
var_dump($nameMatch);
var_dump($multiSpace);
if(count($nameMatch)>0){
if(count($multiSpace)>0){
echo "Invalid Name Multispace";
}
else{
echo "Valid Name";
}
}
else{
echo "Invalid Name";
}
?>
A regex for one to three words consisting of only Unicode letters in PHP looks like
/^\p{L}+(?:\h\p{L}+){1,2}\z/u
Description:
^ - string start
\p{L}+ - one or more Unicode letters
(?:\h\p{L}+){1,2} - one or two sequences of a horizontal whitespace followed with one or more Unicode letters
\z - end of string, even disallowing trailing newline that a dollar anchor allows.

How we check movie is 3d or not from filename using regex

I have certain file in my folder
PARANORMAN_3D_hsbs_.mkv
RunAllNight_Filmbank.mkv
SHUTTER_ISLAND_airwave.mkv
THE_CHANGE_UP_airwave.mkv
The_Avenger_3D_Trailer1.mkv
moviename (year).3d.hsbs.mkv
moviename 3d sbs.mkv
moviename.3D-HTAB.mkv
moviename-3D.sbs-720p.mkv
jurassic_world_airwave_3D_hsb.mkv
As per kodi forum http://forum.kodi.tv/showthread.php?tid=173385
Any of the following terms in the file name should trigger 3D detection:
3DTAB,3D.TAB,HTAB,3DSBS,3D.SBS,HSBS
What I try
<?php
$pattern="(7F7|3DSBS|3D.SBS|HSBS|H.SBS|H-SBS| SBS |FULL-SBS|FULL.SBS|)";
$file_name="PARANORMAN_3D_hsbs_.mkv";
if (preg_match($pattern, $file_name)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
If file name is PARANORMAN_3D_hsbs_ it gives true But if file name PARANORMAN3Dhsbs i want false. My code give me true in every condition
How we check these thing in my file name
You already have the alternation of the different words you want to match:
(7F7|3DSBS|3D.SBS|HSBS|H.SBS|H-SBS| SBS |FULL-SBS|FULL.SBS)
You then write in comments you don't want to match it wihtin words. You also name it that way, that it must follow after a special character but you can't clearly say what are all special characters.
A good working approach with regular expressions in that case is to make use of Word Boundaries, a special anchor written as \b. You can just put it in front of your alternation:
(\b(7F7|3DSBS|3D.SBS|HSBS|H.SBS|H-SBS| SBS |FULL-SBS|FULL.SBS))
^^^ ^
You can also make the alternation non-capturing as you don't need it to capture (see as well Subpatterns to understand the inner parenthesis):
(\b(?:7F7|3DSBS|3D.SBS|HSBS|H.SBS|H-SBS| SBS |FULL-SBS|FULL.SBS))
^^
And you perhaps want to ignore case:
(\b(?:7F7|3DSBS|3D.SBS|HSBS|H.SBS|H-SBS| SBS |FULL-SBS|FULL.SBS))i
^
If the anchor does not work in your case, you can replace it with the regular expression that matches any of your special characters, for example with a character class:
([_-](?:7F7|3DSBS|3D.SBS|HSBS|H.SBS|H-SBS| SBS |FULL-SBS|FULL.SBS))i
This will allow only after underscore or dash and won't allow any of the words in the alternation at the beginning of the filename any longer (which is probably even wanted in your case).
Or with your character class:
([._ ~,](?:7F7|3DSBS|3D.SBS|HSBS|H.SBS|H-SBS| SBS |FULL-SBS|FULL.SBS))i

PHP - preg_match()

Alright, so I want the user to be able to enter every character from A-Z and every number from 0-9, but I don't want them entering "special characters".
Code:
if (preg_match("/^[a-zA-Z0-9]$/", $user_name)) {
#Stuff
}
How is it possible for it to check all of the characters given, and then check if those were matched? I've tried preg_match_all(), but I didn't honestly understand much of it.
Like if a user entered "FaiL65Mal", I want it to allow it and move on. But if they enter "Fail{]^7(,", I want it to appear with an error.
You just need a quantifier in your regex:
Zero or more characters *:
/^[a-zA-Z0-9]*$/
One or more characters +:
/^[a-zA-Z0-9]+$/
Your regex as is will only match a string with exactly one character that is either a letter or number. You want one of the above options for zero or more or one or more, depending on if you want to allow or reject the empty string.
Your regular expression needs to be changed to
/^[a-zA-Z0-9]{1,8}$/
For usernames between 1 and 8 characters. Just adjust the 8 to the appropriate number and perhaps the 1.
Currently your expression matches one character
Please keep in mid that preg_match() and other preg_*() functions aren't reliable because they return either 0 or false on fail, so a simple if won't throw on error.
Consider using T-Regx:
if (pattern(('^[a-zA-Z0-9]{1,8}$')->matches($input))
{
// Matches! :)
}

Make sure username is not a phone number

I'm writing a mobile website and I would like the user to be able to login via username or phone number. I think the easist way to validate their response it to not allow them to signup using a phone number as their user name.
The problem is that I'll need to check if the input of the username field is JUST a 10 or 11 digit number. This is where my inexperance in regex comes to my disadvantage. I'm hoping to try something like
function do_reg($text, $regex)
{
if (preg_match($regex, $text)) {
return TRUE;
}
else {
return FALSE;
}
}
$username = $_POST['username'];
if(do_reg($username, '[0-9]{10,11}')){
die('cannot use a 10 or 11 digit number as a username');
}
The above regex is matching all numbers that are 10-11 digits long. I think maybe I need a way to say if the ONLY thing in the user input field is a 10-11 digit number get mad otherwise release the butterflies and rainbows.
EDIT: For the record I decided to make sure the username wasn't JUST a number. Thought this would be simpler and I didn't like the idea of having people use numbers as logins.
So I ended up with
if (!empty($username) && preg_match('/^\d+$/', $username )) {
die('username cannot be a number');
}
Thanks for the help all.
You are almost correct, except PCRE in PHP requires delimiters, and probably some anchors to make sure the field consists only of numbers.
if(do_reg($username, '/^\d{10,11}$/')){
// ^^ ^^
And probably use \d instead of [0-9].
(BTW, you should just call preg_match directly:
if (!preg_match('/^\d{10,11}$/', $username)) {
release('bufferflies', 'rainbows');
}
You need to anchor the regex to match the entire string: ^[0-9]{10,11}$.
^ matches the beginning of a string; $ matches the end.
Limit usernames to only 10 characters and require there username to start with a letter. How would a user write a 10 digit phone number as their username if they are required to enter in at least 1 alpha character (since phone numbers can't start with a 0/o or a 1/l)? (Heck I would require at least 3 alpha chars just to be safe).
When your app gets bigger then you can allow for longer usernames and take into account some of these issues:
Do not use ^ or $ signs if you are only testing the username: if(do_reg($username, '/^\d{10,11}$/')){
The reason I say this is anyone could defeat that by placing a letter in their username, a1235551212
instead use this:
if(do_reg($username, '/\d{10,11}/')){ because that will flag a1235551212d
Also, importantly, remember, that all of these regular expressions are only checking for numbers, there's nothing to stop a user from doing the following: ltwo3for5six7890. Unless of course you limit the username size.
You just should include start and end of the string in the regex
^[0-9]{10,11}$

how to validate an email address to this form: 234903284#student.uws.edu.au

I'd like to know a regex that would validate:
an email address to this form: 234903284#student.uws.edu.au
couple issues:
"student." is optional and could be any word eg "teacher.".
"324234234" can be any alpha numeric characters (number, word, _ etc.)
the email must end in "uws.edu.au"
This is what I have so far:
/(\d*)#\w*\.uws\.edu\.au/
valid addresses:
me#uws.edu.au
234234324#student.uws.edu.au
theking#teacher.uws.edu.au
etc.
Thanks Guys
Three thoughts:
Change the initial \d to \w to match "word" characters [a-zA-Z0-9_] instead of just digits.
Make the subdomain optional using ?
Use + instead of * when matching the username and subdomain. Otherwise #.uws.edu.au will validate.
Suggested:
/\w+#(\w+\.)?uws\.edu\.au/
You said:
Just tried /(\w*)#(\w*.)?uws.edu.au/ and that seemed to work. Any further suggestions are welcome – Jason 4 secs ago
Your regex will match "#teacher.uws.edu.au" (i.e. "name portion" omitted).
To fix this, you could use:
/(\w+)#(\w+\.)?uws\.edu\.au/
Which will require at least one character in the name portion, and at least one char before the dot (if there is a dot) in the subdomain spot.
Also (I think) that \w will not match . (and probably other chars that you care about in the name portion too), so bob.jones#student.uws.edu.au would fail to match. The following would add the char ., _, and - into the "name" portion:
/([\w\._-]+)#(\w*\.)?uws\.edu\.au/
you could add any other chars you need in the same way.
NOTE: Matching email addresses in general a more complex thing than you might think (lots of strange things are technically allowed in email addresses. Here is an article on the subject (There are many other sources of similar information available).

Categories