Python Regular Expressions

>>> urls = r'/node/(\d)'
>>> regex = re.compile(urls)
>>> regex.search('/node/122').group()
'/node/1'
>>> regex.search('/node/122').group(1)
'1'
  • define the pattern.
  • compile, python will return regex object
  • use the search method, pass the string we wanna search for.
  • match object will be return if the pattern found.
  • use .group() method of match object to pull out matching group.
 
python/regex.txt · Last modified: 2007/10/31 01:08 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki