'Find a string and replace it with another string with regex?
I have a text file with below format:
Available Text1
Id = 0005
Cause = ERROR
Code = 307
Event Time = 2020-11-09 10:16:48
Severity = WARNING
Severity Code = 5
result = 2015-06-12 10:19:47
Id = 0006
Cause = FAILURE
Code = 517
Event Time = 2020-11-09 10:19:47
Severity = MINOR
Severity Code = 4 result = noresult
ip[10.1.1.1
I want to find result = in each text file and fulfill the value with the IP.
Requested Data
Id = 0005
Cause = ERROR
Code = 307
Event Time = 2020-11-09 10:16:48
Severity = WARNING
Severity Code = 5
result = 10.1.1.1
Id = 0006
Cause = FAILURE
Code = 517
Event Time = 2020-11-09 10:19:47
Severity = MINOR
Severity Code = 4
result = 10.1.1.1
ip[10.1.1.1
I used the below code, but it only finds no result value and replace it with IP however there is a datetime
value (2015-06-12 10:19:47
) with comes in another line as the value of result. How can I use a regex to first find any value after result=
and then replace it with IP
?
for filepath in file_list:
txt = open(filepath, "r").read()
txt = txt.replace("noresult", txt.split("ip[")[-1])
open(filepath, "w").write(txt)
Solution 1:[1]
I would suggest to try with str.rfind
or str.rsplit
along with re.sub
:
import re
s = """\
Id = 0005
Cause = ERROR
Code = 307
Event Time = 2020-11-09 10:16:48
Severity = WARNING
Severity Code = 5
result = 2015-06-12 10:19:47
Id = 0006
Cause = FAILURE
Code = 517
Event Time = 2020-11-09 10:19:47
Severity = MINOR
Severity Code = 4 result = noresult
ip[10.1.1.1
"""
# len('ip[') == 3
ip_addr = s[s.rfind('ip[') + 3:].rstrip('\n')
s = re.sub(r'(result\s*=).*', rf'\1 {ip_addr}', s)
print(s)
Out:
Id = 0005
Cause = ERROR
Code = 307
Event Time = 2020-11-09 10:16:48
Severity = WARNING
Severity Code = 5
result = 10.1.1.1
Id = 0006
Cause = FAILURE
Code = 517
Event Time = 2020-11-09 10:19:47
Severity = MINOR
Severity Code = 4 result = 10.1.1.1
ip[10.1.1.1
Solution 2:[2]
So you want to replace result=ANYTHING to result=IP. In this case you may use re.sub() with a regular expression that matches every thing after "result=".
import re
re.sub(r"result =[^\n]+","result = ip[10.1.1.1",txt)
The [^\n]+
will match every char in a single line.
To extract the IP from the text and do the replacement:
ip=re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b",txt)[0]
txt=re.sub(r"result =[^\n]+","result = "+ip)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |