Reputation: 609
I have a file which contains the below logs. Now I want to find out where my TD_Map
string is. I can achieve that through str.find()
method. But is there a way to get the complete string as a return value? Like for this example I must get TD_Map2
.
I have to search TD_Map
string only because the rest part can be any integer constant.
Running
OK, start the cluster assignment.
: 6035
Warning: The Default Cluster size results in a cluster with multiple Amps on the same Clique.
The cluster assignment is complete.
Reading
da 6-7
Running
OK.
The AMPs 6 to 7 are deleted.>
Reading
ec
Running
OK.
The session terminated. TD_Map2 is saved.>
Upvotes: 1
Views: 31
Reputation: 21893
There are so many ways to do this, but it seems like a nice use case for regex:
import re
s = """Running
OK, start the cluster assignment.
: 6035
Warning: The Default Cluster size results in a cluster with multiple Amps on the same Clique.
The cluster assignment is complete.
Reading
da 6-7
Running
OK.
The AMPs 6 to 7 are deleted.>
Reading
ec
Running
OK.
The session terminated. TD_Map22 is saved.>
"""
match = re.search('TD_Map([\d]+)', s)
print(match.group(0)) # Print whole "TD_Map2"
print(match.group(1)) # Print only the number "2"
Output:
TD_Map2
2
Upvotes: 1