Issue
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.
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)
Answered By - Alberto Garcia
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.