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

Why is this variable not being evaluated?

I want to create a python function that receives 2 strings, I want to search the first within the second.

#Function to evaluate a string in a multiline string
def checkString(mystring, maintext):
    result = re.findall(r'^mystring', maintext, re.MULTILINE)
    if len(result) == 0:
        return False
    else:
        return True

My variable "mystring" is not being evaluated, I always get False. I know the re.findall works because if I take out of the function, it works, so I am evaluating this wrong.

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

>Solution :

you need to template the variable mystring in your regexp: substitute

    result = re.findall(r'^mystring', maintext, re.MULTILINE)

with an f-string:

    result = re.findall(rf'^{mystring}', maintext, re.MULTILINE)
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