M.salameh
M.salameh

Reputation: 109

extract text from regular expression

I am new with regular expressions. I want to extract timestamp value from this text

chunk =['leaset loaded server= 10, timestamp= 1494370749.94']
try:
     found = re.findall('\['+'+ timestamp= +'+'\]', chunk).group(1)
  except AttributeError:

     found = 'Not found'

this gave me not found. what is the correct statement for extracting it.

Upvotes: 1

Views: 82

Answers (1)

tdelaney
tdelaney

Reputation: 77347

You seem to be mixing up findall and search. While you could write it either way, search will stop after it finds the string and so its a bit faster.

>>> import re
>>> chunk = 'leaset loaded server= 10, timestamp= 1494370749.94'
>>> match = re.search(r'timestamp= ([\d\.]+)', chunk)
>>> if match:
...     print(match.group(1))
... 
1494370749.94

Upvotes: 1

Related Questions