-
-
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 |
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.
Hello! How can I make this work with PHP´s preg_match() function? Thanks for help.
@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
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"
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.
@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