tags:

views:

36

answers:

2

Hello,

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

+1  A: 
r = re.compile(r"urn:uuid:5EEF382F-JSQ9-3c45-D5E0-K15X8M8K76:\[u'(.*)'\]")
m = r.search(str(serv))
if m:
    print 'Found', m.group(1)
Marcelo Cantos
@Marcelo Cantos -- Thanks for the response - I tried this but it doesn't find anything. It can't find a match. It happened with the answer from ghostdog as well which is strange. Can you think why this is? Could it be with the way the variable is created? Thanks
chrissygormley
What is the type of `serv`?
Marcelo Cantos
+1  A: 

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'
ghostdog74
@ghostdog -- Thanks for the response - I tried this but it doesn't find anything. It can't find a match. It happened with the answer from Marcelo as well which is strange. Can you think why this is? Could it be with the way the variable is created? Thanks
chrissygormley
try the way i do it with the interpreter.
ghostdog74
@ghostdog -- The variable was not being output as a string properly. But your answer had some of the code that I found useful, regular expression wasn't required. thanks
chrissygormley