Limits on search length
Hrvoje Niksic
hniksic at xemacs.org
Mon Oct 1 14:28:32 EDT 2007
Daryl Lee <dlee at altaregos.com> writes:
> 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.
It seems to work for me. Which version of Python are you using?
Here is how I tested it. First, I modified your program so that it
actually runs (sys and re imports were missing) and removed
unnecessary globbing and file opening:
import sys, re
searchPattern = sys.argv[1]
cpat = re.compile(searchPattern)
lineNumber = 0
for line in sys.stdin:
lineNumber += 1
m = cpat.search(line)
if m is not None:
print "(", lineNumber, ")", line
Now, create a file with three lines, each with a string of different
length:
$ printf '"%*s"\n' 271 > fl
$ printf '"%*s"\n' 272 >> fl
$ printf '"%*s"\n' 273 >> fl
And run the script:
$ python scriptfile '".{272}"' < fl
( 2 ) "[... 272 blanks]"
That looks correct to me.
> In the short term, I can resort to locating the character positions
> of the quotes,
You can also catch all strings and only filter those of the length you
care about.
More information about the Python-list
mailing list