Home > Blockchain >  Read all the words starting from substring
Read all the words starting from substring

Time:06-30

I have csv file in which keywords are given. I have to match all the words starting with the keywords from the text.

text = "1 who were randomized 1 1 to daro 600 mg twice daily or matching pbo in addition to adt docetaxel randomization was stratifi
ed by extent of disease according to tnm m1a vs m1b vs m1c and alkaline phosphatase levels vs ≥ upper limit of normal the primary endpoint was os secondary efficac
y endpoints included time to crpc time to pain progression time to first symptomatic skeletal event sse and time to initiation of subsequent systemic antineoplasti
c therapies safety was also assessed resu from nov 2016 to june 2018 1306 pts were randomized 651 to daro"

keyword = ["random"]

So here I want to read all the words starting with random

CodePudding user response:

Use re.findall along with the regex pattern \brandom\w*:

text = "1 who were randomized 1 1 to daro 600 mg twice daily or matching pbo in addition to adt docetaxel randomization was stratified by extent of disease according to tnm m1a vs m1b vs m1c and alkaline phosphatase levels vs ≥ upper limit of normal the primary endpoint was os secondary efficacy endpoints included time to crpc time to pain progression time to first symptomatic skeletal event sse and time to initiation of subsequent systemic antineoplastic therapies safety was also assessed resu from nov 2016 to june 2018 1306 pts were randomized 651 to daro"

keywords = ["random"]
regex = r'\b(?:'   r'|'.join(keywords)   ')\w*'
matches = re.findall(regex, text)
print(matches)  # ['randomized', 'randomization', 'randomized']

CodePudding user response:

text = "1 who were randomized..."
for word in text.split(" "):
    if word.startswith("random"):
        print(word)
  • Related