Limits on search length

Daryl Lee dlee at altaregos.com
Mon Oct 1 13:16:49 EDT 2007


I am trying to locate all lines in a suite of files with quoted strings of 
particular lengths.  A search pattern like r'".{15}"' finds 15-character 
strings very nicely.  But I have some very long ones, and a pattern like 
r'".{272}"' fails miserably, even though I know I have at least one 
272-character string.

In the short term, I can resort to locating the character positions of the 
quotes, but this seemed like such an elegant solution I hate to see it not 
work.  The program is given below (sans imports), in case someone can spot 
something I'm overlooking:

# Example usage: search.py *.txt \".{15}\"

filePattern = sys.argv[1]
searchPattern  = sys.argv[2]
cpat = re.compile(searchPattern)

for fn in glob.glob(filePattern):
     f = open(fn, "r")
     if f:
         lineNumber = 0
         for line in f:
             lineNumber += 1
             m = cpat.search(line)
             if m is not None:
                 print fn, "(", lineNumber, ")", line
     f.close()


-- 
Daryl Lee
Open the Present -- it's a Gift!




More information about the Python-list mailing list