I have one input file. sample.txt: is a text file that contains multiple functions with parameters and values.
Sample.txt:
step(change_lane in [1.5..2]s) # From: 'change_lane: [1.5..2]s'
step(stop_lane in [1.8..21]s) # From: 'stop_lane: [1.5..2]s'
step(stay_lane in [1.7..9]s) # From: 'stay_lane: [1.5..2]s'
The new lane change is == 25
.......
My code is the following:
for line in open('sample.txt'):
match = re.search('change_lane', line)
if match:
# Finds numbers in line, outputs a list of numbers in line
x = re.findall(r'(\d+(?:\.\d+)?)', line)
# Gets first/second number from list
print(f"This is line: {x}")
get_num1 = x[0]
get_num2 = x[1]
#Replace first number found with number 200
rep_num1 = re.sub(r'\b'+get_num1+r'\b','200',line)
print(f"Rep1: {rep_num1}")
#Replace second number found with number 17.21 under new edited line.
rep_num2 = re.sub(r'\b'+get_num2+r'\b','17.21',rep_num1)
#Old text line will equal to edited line
line = rep_num2
print(f"This is the new line: {line}")
The code will search through the text and match a phrase in this case "change_lane". If it matches it will find all numbers and output them as a list: [1.5,2,1.5,2].I am only interested in changing the first two numbers as the rest is a comment. Next, I will replace first number in the list with number 200. Now rep_num1 will become the new line as such:
keep(change_lane in [200..2]s) # From: 'change_lane: [200..2]s
help here: Then, I want the code to replace the number 2 with 17.21 as such:
keep(change_lane in [200..17.21]s) # From: 'change_lane: [200..17.21]s
But instead it replaces all number '2's with 17.21 as such:
keep(change_lane in [17.2100..17.21]s) # From: 'change_lane: [17.2100..17.21]s
How can I write the regular expression to find the exact number and replace that number even if the number is repeated?