I have multiple files, each containing multiple strings like this one:
Species_name_ID:0.0000010229,
I need to find the string with a specific 'Species_name_ID', that I ask the user to provide, and do a simple replacement so that it now reads:
Species_name_ID:0.0000010229 #1,
I'm stuck at the first part, trying to look for the pattern. I've tried looking only for the numeric pattern at the end with this, and it returns a list of all the instances in which the pattern appears:
my_regex = r':0\.\d{10}'
for line in input_file:
sp = re.findall(my_regex, line)
print(sp)
However, when I try adding the rest by using the string the user provides, it doesn't work and returns an empty list.
search = input("Insert the name of the species: ")
my_regex = f"{search}:0\.\d{{10}}"
for line in input_file:
sp = re.findall(my_regex, line)
print(sp)
I've also tried the following syntax for defining the variable (all come from this previous question How to use a variable inside a regular expression?):
my_regex = f"{search}"
my_regex = f"{search}" + r':0\.\d{10}'
my_regex = search + r':0\.\d{10}'
my_regex = re.compile(re.escape(search) + r':0\.\d{10}')
my_regex = r'%s:0\.\d{10}'%search
my_regex = r"Drosophila_melanogaster_12215" + r':0\.\d{10}'
Even when I try searching for the specified string, it doesn't find it in the file even when there are multiple hits it could make.
my_regex = Drosophila_melanogaster_12215
What am I missing?