I am creating a python function with two inputs: a file and a string, in which user can find the location of the string in the file. I figured the best way to do this would be with regular expressions. I have converted the file to one big string (file_string) earlier in the code. For example, let's say the user wants to find "hello" in the file.
input = "hello"
user_input = "r'(" + input + ")'"
regex = re.compile(user_input)
for match in regex.finditer(file_string):
print(match.start())
Creating a new string with r' ' around the input variable is not working. However, the code works perfectly if I replace user_input with r'hello'. How can I convert the string input the user enters to an expression that can be put into re.compile()?
Thanks in advance.