Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex for LinkedIn profile

I have want to have an function that replace a LinkedIn profile that i found in a string.

example:

After using the function it should be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • You can find my linkedin [Linkedin]
strA ='[Linkedin]'

Def linkedin(sentence):
urlReg = "^https?:\/\/?(w{3}.)? linkedin\.\/.$"
res = re.search(urlReg, sentence)
print(res)
if res != None:
    ## replace urls
    sentence= re.sub(urlReg,strA, sentence)
return (sentence)

When i print (res) it does not taking the string in, i think my Regex is not correct

>Solution :

A few notes about the pattern that you tried:

  • There is no // in https:www.linkedin.com/in/kim-zand-3126573/ (maybe a typo?)
  • linkedin\.\/ does not match linkedin.com as \/ matches /
  • There is no space in the example string, that is expected in the regex before linkedin\.
  • The text is not at the start of the string ^ while there is an anchor
  • You have to escape the dot to match it literally
  • The second forward slash in // does not have to be optional and you don’t have to escape the forward slash

You could for example match:

\bhttps?://www\.linkedin\.com/\S*

Or if there is really no double forward slash in the data:

\bhttps?:www\.linkedin\.com/\S*

And then replace with [Linkedin]

See a regex demo

Or a bit more specific match for the given example, matching the -digits part before the last /

\bhttps?://www\.linkedin\.com\/[^\s/]+/[^/\s]+-\d+/(?!\S)

See another regex demo

Example:

import re

strA = '[Linkedin]'
urlReg = re.compile(r"\bhttps?://www\.linkedin\.com/\S*")

def linkedin(sentence):
    return urlReg.sub(strA, sentence)

result = linkedin(r"You can find my linkedin https://www.linkedin.com/in/kim-zand-3126573/")
print(result)

Output

You can find my linkedin [Linkedin]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading