Home > Blockchain >  Get link which matches exact word
Get link which matches exact word

Time:08-07

Hi I have many links from which I only need links which match exact words like join, career etc.

For example

links = ['https://enzymocore.com/news/august-2015-joint-venture-in-peru/'  ,  'https://enzymocore.com/join-us'].

So if my keyword is join then I should only get the second link not the first one. Here is my current code:

for link in links:
   if 'join' in link:
      finallink.append(link)

but from this code I am getting both because in first link the word 'joint' contain 'join'.

CodePudding user response:

How about this regex check? If any letter (a to z or A to Z) at start of at end of the target like join or career then the link is not a match.

import re

final_links = []

links = [
    'https://enzymocore.com/news/august-2015-joint-venture-in-peru/',
    'https://enzymocore.com/join-us'
]

for link in links:
    if re.search(r"[^a-zA-Z](join|career)[^a-zA-Z]", link):
        print(link)
        final_list.append(link)

  • Related