Use custom regex to validate linkedIn url

Viewed 1056

I am trying to normalize linkedin urls. For that I am using regex ^https:\/\/[w]{3}\.linkedin\.com\/.*$ which needs to change according to new requirement. The only valid patterns are:

  • https://www.linkedin.com/in/XXXXXX-XXXXX-55301b41
  • https://www.linkedin.com/pub/XXXXXX-XXXXX-55301b41
  • https://www.linkedin.com/public-profile/in/XXXXXX-XXXX-b82a7b10a
  • https://www.linkedin.com/public-profile/pub/XXXXXX-XXXX-b82a7b10a

Few bad corner cases are:

  • https://www.linkedin.com/in/xxxx-xxxx-ab85a328/%7Bcountry%3Dde%2C+language%3Dde%7D?trk=people-guest_profile-result-card_result-card_full-click
  • https://www.linkedin.com/edu/school?id=18987
  • https://www.linkedin.com/in/xxxx-xxxx-55301b41/de
  • https://www.linkedin.com/pub/xxxxx-xxxxx/98/b7a/22b
  • https://it.linkedin.com/public-profile/in/xxxx-xxxxx-80520667?challengeId=AQEV3tHveORYsAAAAXQqx1N3bZ7fd44s5ngegJp6rC0UoWvhG2LtAhhgld8h3QyBxorfcLL2iuvK4xh_UKoGguxvX6sFx_nnZA&submissionId=7bc972ed-bfd1-2e16-c395-ad35cb550117

The logics would be:

  • must start with https://www.linkedin.com/
  • must have /in/, /pub/, /public-profile/in/, /public-profile/pub
  • should end with username i.e XXXXXX-XXXXX-55301b41
2 Answers

With your shown samples only, could you please try following regex. Online demo of regex is: Online demo of regex

^http[s]?:\/\/www\.linkedin\.com\/(in|pub|public-profile\/in|public-profile\/pub)\/([\w]{6}-[\w]{1,}-[\w]+)$

Explanation: Adding detailed explanation for above.

^http[s]?:                    ##Checking if URL starts from http OR https.
\/\/www\.linkedin\.com\/      ##Then checking if domain is www.linkedin.com
(in|pub|public-profile\/in|public-profile\/pub)  ##Then checking if its followed by in OR pub OR public-profile/in OR public-profile/pub
\/([\w]{6}-[\w]{1,}-[\w]+)$   ##Checking if above is followed by / [\w] with 6 occurrences - [\w] with 1 or more occurrences and then [\w] with 1 or more occurrences.

NOTE: In case you want to check url should only start with https then change ^http[s]? TO ^https in above regex.

NOTE2: Above will create 2 capturing groups, in case you don't want to create any capturing groups try following.

^http[s]?:\/\/www\.linkedin\.com\/(?:in|pub|public-profile\/in|public-profile\/pub)\/(?:[\w]{6}-[\w]{1,}-[\w]+)$

Regex demo for note2 regex

Instead of using a regex for the complete string, try using a url parser first, then validate the scheme, netloc, and path separately

>>> import urllib.parse
>>> urllib.parse.urlparse("https://www.linkedin.com/in/XXXXXX-XXXXX-55301b41")
ParseResult(scheme='https', netloc='www.linkedin.com', path='/in/XXXXXX-XXXXX-55301b41', params='', query='', fragment='')
>>> urllib.parse.urlparse("https://www.linkedin.com/in/xxxx-xxxx-ab85a328/%7Bcountry%3Dde%2C+language%3Dde%7D?trk=people-guest_profile-result-card_result-card_full-click")
ParseResult(scheme='https', netloc='www.linkedin.com', path='/in/xxxx-xxxx-ab85a328/%7Bcountry%3Dde%2C+language%3Dde%7D', params='', query='trk=people-guest_profile-result-card_result-card_full-click', fragment='')

component validation

>>> parsed_url = urllib.parse.urlparse("https://www.linkedin.com/in/XXXXXX-XXXXX-55301b41")
>>> parsed_url.scheme == "https"
True
>>> parsed_url.netloc.endswith(".linkedin.com")
True
Related