Python-Ref > Regular expressions (regexp) > Regexp strategies I.
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Regexp strategies I.

How to use regexps effectively - be specific
When searching for a particular string in a text, it is good to be as specific as possible, especially if you don't know the exact structure of the text. I would recommend the following three guidelines.
  • When you are sure the data you search for do not span to more than one line, consider iterating over the lines and applying the regexp to each of them. This might save problems with multiline searches.
  • Specify as much as possible about the text you search for, it might save you unwelcome surprises later.
  • Try and debug you regular expressions.
The following example demonstrates all the three approaches.
Expand/Shrink
Zdroj: (regexp11-1.py)
  1   import re
  2   
  3   text = '''# this is a python code inside a string
  4   import sys
  5   print sys.argv
  6   printer = {}
  7   for i in range(10):
  8     printer[i] = chr(i+65)
  9     #print i
 10     # print printer[i]
 11     # here we do not print anyway
 12   no_print = chr(1)
 13   sys.exit()
 14   '''
 15   # we would like to extract all lines with print statements from the code above
 16   
 17   # simple approach
 18   for line in text.splitlines():
 19     if re.search( "print", line):
 20       print line
 21   
 22   print "--------------------"
 23   
 24   # better approach
 25   for line in text.splitlines():
 26     if re.search( "print\s+", line):
 27       print line
 28   
 29   print "--------------------"
 30   
 31   # finally working
 32   for line in text.splitlines():
 33     if re.search( "^\s*print\s+", line):
 34       print line
stdout:
print sys.argv
printer = {}
  printer[i] = chr(i+65)
  #print i
  # print printer[i]
  # here we do not print anyway
no_print = chr(1)
--------------------
print sys.argv
  #print i
  # print printer[i]
  # here we do not print anyway
no_print = chr(1)
--------------------
print sys.argv
Doba běhu: 15.3 ms