Skip to content

Instantly share code, notes, and snippets.

@technion
Created October 17, 2015 08:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save technion/5ca01ca420725e17cd3f to your computer and use it in GitHub Desktop.
Save technion/5ca01ca420725e17cd3f to your computer and use it in GitHub Desktop.
def instacheck(name)
unless /[a-z0-9._]{,30}/.match(name)
return false
end
if /\.\./.match(name)
return false
end
if /^\d+$/.match(name)
return false
end
if /^\./.match(name)
return false
end
true
end
@yizeng
Copy link

yizeng commented May 31, 2017

Just a heads up for who came in from search engine.

As of May 2017, Instagram doesn't allow leading or trailing dot in the usernames, nor consecutive dots.

This regex by Jonathan Stassen works for me. Source http://blog.jstassen.com/2016/03/code-regex-for-instagram-username-and-hashtags

^([A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:\.(?!\.))){0,28}(?:[A-Za-z0-9_]))?)$

@JonasSchroeder
Copy link

@yizeng
Thank you very much!
I modified it a bit so it includes the @ sign (which it needs to do for my task) and works in R:

@([A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:\\.(?!\\.))){0,28}(?:[A-Za-z0-9_]))?)

Best
Jonas

@kevinpareek
Copy link

kevinpareek commented Nov 27, 2019

@yizeng and @JonasSchroeder

your regex not correct please modified for dot username like 'dreaming.of_makeup'

my modified:
^([A-Za-z0-9._](?:(?:[A-Za-z0-9._]|(?:\.(?!\.))){2,28}(?:[A-Za-z0-9._]))?)$

thank you!
kevin

@LiLPandemio
Copy link

that does not work for @jhon.potter :/

@nptl
Copy link

nptl commented May 20, 2021

I needed to match and extract the username from instagram profile URL.
Built this myself and it works smooth:

(?<=instagram.com\/)[A-Za-z0-9_.]+

Checkout the demo here - regexr.com/5tbdb
You can modify characters as per your usecase.

Screenshot 2021-05-20 at 5 22 46 PM

@verstecken
Copy link

Hello! How can I make this work with PHP´s preg_match() function? Thanks for help.

@technion
Copy link
Author

technion commented Feb 3, 2022

@verstecken You would pick one of several options in this thread and paste the regex into your function as per its manual page: https://www.php.net/manual/en/function.preg-match.php

@verstecken
Copy link

verstecken commented Feb 3, 2022

Thanks, @technion I tried that before and didn't get it.
You will need to delimit your pattern with for example '/' in preg_match() and it is not explained on the functions manual website. Maybe a basic to know? Well, that`s why I asked here, because pasting the regex from above did not work straight. I found some info here: https://infoheap.com/php-regex-delimiter-examples/

If you want to get the username out of an instagram or twitter URL, in PHP, this works well:

$url = "https://instagram.com/some.username";
$pattern = "/(?:(?:http|https):\/\/)?(?:www\.)?(?:instagram\.com|instagr\.am|twitter\.com)\/([A-Za-z0-9-_\.]+)/im";

preg_match($pattern, $url, $matches);
echo $matches[1]; // Yeah! This returns "some.username"

@technion
Copy link
Author

technion commented Feb 4, 2022

My regex did no include any such characters. I can't comment on subsequent advice from anyone else. My code already checked for leading dots and did not fail any tests people brought up relating to someone elses code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment