I have want to have an function that replace a LinkedIn profile that i found in a string.
example:
- You can find my linkedin https:www.linkedin.com/in/kim-zand-3126573/
After using the function it should be:
- 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
//inhttps: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]