12

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.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
UnderPhp
  • 328
  • 1
  • 2
  • 14

9 Answers9

20

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!

luukvhoudt
  • 1,726
  • 19
  • 33
  • 3
    One more requirement would be that username's cant contain only numbers. – Hansel Apr 07 '21 at 22:07
  • 3
    @Hansel simply update it to reflect that: `^[a-zA-Z_](?!.*?\.{2})[\w.]{1,28}[\w]$` – Pezhvak Jan 20 '22 at 22:22
  • @Pezhvak, your ^ update in your comment does not allow the first character to be a number when it should. Otherwise it's perfect. Can you modify to allow the first character to be a number while still denying entire strings that are only numbers? – DevMike Jan 27 '22 at 11:19
  • @DevMike well, you're right, it can contain numbers for first character, the original answer is the way to go. to prevent username to contain only numbers i think solution is to use another expression after the first one since i cannot think of a regex to contain both. – Pezhvak Jan 27 '22 at 14:54
  • Right, a subsequent expression! makes sense. – DevMike Jan 28 '22 at 11:25
  • Looks like the minimum length is 1 character. At least [instagram.com/a](https://www.instagram.com/a/) and [instagram.com/c](https://www.instagram.com/c/) accounts exist. [source](https://stackoverflow.com/questions/15470180/character-limit-on-instagram-usernames#comment57548703_17087528) – Flimtix Apr 27 '22 at 10:44
  • I think accents are not allowed either (á, é, í, ó, ú) – Joseph Sep 02 '23 at 16:32
14

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_]))?)
Jeff
  • 179
  • 1
  • 4
8

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");
}
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • 10
    This is wrong, because you cannot start or end with dot (`.`), and it seems like you cannot have consecutive dots either. – beruic Apr 28 '16 at 09:16
1

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.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
1

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:

  1. @: Find an @ symbol (optional)
  2. ([\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)
  3. (?: this before the above just means "don't actually create a capturing group for this"
  4. [\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

lufc
  • 1,965
  • 2
  • 15
  • 19
0

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");
 }
NextZuck
  • 35
  • 4
0

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");
}
mamal
  • 1,791
  • 20
  • 14
  • Why are you escaping dot `.` and underscore `_`? Why do you include pipe `|` in the character class? Why do you add `i` and `m` flags? – Toto Feb 10 '20 at 12:43
  • @Toto pipe mean or in regex and `i` mean caseinsensitive in string and `m` mean multi line It may have been used to scape these characters `_` ,dot sign – mamal Feb 10 '20 at 12:50
  • 1
    You're wrong. I suggest you to read a [regex tutorial](https://www.regular-expressions.info/) before answering. Pipe in a character class means pipe, the `i` flag is useless here because you have `a-z` & `A-Z` OP doesn't talk about multiline. Moreover your code throws an exception for valid username. – Toto Feb 10 '20 at 12:56
0

Reg Useful regex

(?!.*\.\.)(?!.*\.$)[^\W][\w.]{0,29}$
Huda
  • 1
  • 2
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – helvete Jan 30 '23 at 12:27
0

try This regex should work for you:

/^[A-Za-z]+(?:[._][A-Za-z0-9]+)*$/u
Abdulmajeed
  • 552
  • 7
  • 8