I am trying to match information stored in a variable. I have a list of uuid's and ip addresses beside them. The code I have is:
r = re.compile(r'urn:uuid:5EEF382F-JSQ9-3c45-D5E0-K15X8M8K76')
m = r.match(str(serv))
if m1:
print'Found'
The string serv contains is:
urn:uuid:7FDS890A-KD9E-3h53-G7E8-BHJSD6789D:[u'http://10.10.10.20:12365/7FDS890A-KD9E-开发者_高级运维3h53-G7E8-BHJSD6789D/']
---------------------------------------------
urn:uuid:5EEF382F-JSQ9-3c45-D5E0-K15X8M8K76:[u'http://10.10.10.10:42365']
---------------------------------------------
urn:uuid:8DSGF89S-FS90-5c87-K3DF-SDFU890US9:[u'http://10.10.10.40:5234']
---------------------------------------------
So basically I am wanting to find the uuid string and find out what it's address is and store it as a variable. So far I have just tried to get it to match the string to no avail. Can anyone point out a solution to this.
Thanks
r = re.compile(r"urn:uuid:5EEF382F-JSQ9-3c45-D5E0-K15X8M8K76:\[u'(.*)'\]")
m = r.search(str(serv))
if m:
print 'Found', m.group(1)
your regex is very simple, so much so that there's no need to use regular expression at all.
>>> serv="""
... urn:uuid:7FDS890A-KD9E-3h53-G7E8-BHJSD6789D:[u'http://10.10.10.20:12365/7FDS890A-KD9E-3h53-G7E8-BHJSD6789D/']
... ---------------------------------------------
... urn:uuid:5EEF382F-JSQ9-3c45-D5E0-K15X8M8K76:[u'http://10.10.10.10:42365']
... ---------------------------------------------
... urn:uuid:8DSGF89S-FS90-5c87-K3DF-SDFU890US9:[u'http://10.10.10.40:5234']
... ---------------------------------------------
... """
>>> tomatch="urn:uuid:5EEF382F-JSQ9-3c45-D5E0-K15X8M8K76"
>>> for row in serv.split("\n"):
... if tomatch in row:
... print row[ row.find("[")+1 : ].replace("]","")
...
u'http://10.10.10.10:42365'
精彩评论