Parsing the subversion Id keyword with python

19 February, 2008 - 11:07

Another one for the category 'remainder-to-self' or 'write-it-on-a-sticky-note-somewhere'.
Parsing a subversion Id keyword is just a matter of some regular expression magic:

# use the built in regular expression library
import re
 
# the subversion Id keyword
svnid = '$Id: svnidparse.py 1234 2008-02-19 09:59:27Z joske $'
 
# bow for the mighty regular expression
svnidrep = r'^\$Id: (?P<filename>.+) (?P<revision>\d+) (?P<date>\d{4}-\d{2}-\d{1,2}) (?P<time>\d{2}:\d{2}:\d{2})Z (?P<user>\w+) \$$'
 
# parse the svn Id
mo = re.match(svnidrep, svnid)
 
# use it, like for example:
print 'this is %s - revision %s (%s)' % mo.group('filename', 'revision', 'date')

hmm, calling it "black magic" would be better, probably.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed
  • Lines and paragraphs break automatically.

More information about formatting options