DebugPointer
Published on

Regex for Linkedin Profile Url

Regex for Linkedin Profile Url

LinkedIn is a social media platform specifically designed for professional networking and job seeking. It allows users to create a profile and connect with other professionals in their industry or field, share information and professional content, and search and apply for jobs. LinkedIn also offers tools and resources for businesses to find and hire employees, and for individuals to enhance their professional skills and reputation. It is a popular platform among professionals, recruiters, and job seekers, and is used by millions of people around the world to connect, share information, and advance their careers. In this article let's understand how we can create a regex for LinkedIn profile URL and how regex can be matched for a given LinkedIn profile URL.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of alphabets that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Structure of a Linkedin profile url

The Linkedin URL should have the following criteria and structure-

  • It should start with http or https or directly with its subdomain .es, .in, .uk etc.,
  • then it has to be followed by ://
  • then it may or maynot contain www.
  • then it must be followed by domain name linkedin
  • then it will be followed by top level domain(TLD) like .com
  • then it will have profile, pub, in etc., in the url path
  • then it might have a profile name
  • then it can also have query params in the url

Regex for checking if Linkedin profile url is valid or not

Regular Expression for lowercase letter-

/^(http(s)?:\/\/)?([\w]+\.)?linkedin\.com\/(pub|in|profile)\/([-a-zA-Z0-9]+)\/*/gm

Test string examples for the above regex-

Input StringMatch Output
linker.iodoes not match
www.google.comdoes not match
es.linkedin.com/pub/francisco-fernandez/30/14/62matches
http://nl.linkedin.com/in/namematches
http://uk.linkedin.com/pub/some-name/1/1b3/b45/matches
https://www.linkedin.com/in/another-namematches

Here is a detailed explanation of the above regex-

/^(http(s)?:\/\/)?([\w]+\.)?linkedin\.com\/(pub|in|profile)\/([-a-zA-Z0-9]+)\/*/gm

^ asserts position at start of a line
1st Capturing Group (http(s)?:\/\/)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
http matches the characters http literally (case insensitive)
2nd Capturing Group (s)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
s matches the character s with index 11510 (7316 or 1638) literally (case insensitive)
: matches the character : with index 5810 (3A16 or 728) literally (case insensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
3rd Capturing Group ([\w]+\.)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below [\w]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equivalent to [a-zA-Z0-9_])
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
linkedin matches the characters linkedin literally (case insensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
com matches the characters com literally (case insensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
4th Capturing Group (pub|in|profile)
1st Alternative pub
pub matches the characters pub literally (case insensitive)
2nd Alternative in
in matches the characters in literally (case insensitive)
3rd Alternative profile
profile matches the characters profile literally (case insensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
5th Capturing Group ([-a-zA-Z0-9]+)
Match a single character present in the list below [-a-zA-Z0-9]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
- matches the character - with index 4510 (2D16 or 558) literally (case insensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

Hope this article was useful to check if the string is a valid linkedin profile url or not.