Looking for Form Feeds

Erik Max Francis max at alcyone.com
Mon Jan 24 23:01:39 CET 2005


Greg Lindstrom wrote:

> I have a file generated by an HP-9000 running Unix containing form feeds 
> signified by ^M^L.  I am trying to scan for the linefeed to signal 
> certain processing to be performed but can not get the regex to "see" 
> it.  Suppose I read my input line into a variable named "input"
> 
> The following does not seem to work...
> input = input_file.readline()
> if re.match('\f', input): print 'Found a formfeed!'
> else: print 'No linefeed!'
> 
> I also tried to create a ^M^L (typed in as <ctrl>Q M <ctrlQ> L) but that 
> gives me a syntax error when I try to run the program (re does not like 
> the control characters, I guess).  Is it possible for me to pull out the 
> formfeeds in a straightforward manner?

What's happening is that you're using .match, so you're only checking 
for matches at the _start_ of the string, not anywhere within it.

It's easier than you think actually; you're just looking for substrings, 
so searching with .find on strings is probably sufficient:

	if line.find('\f') >= 0: ...

If you want to look for ^M^L, that'd be '\r\f':

	if line.find('\r\f') >= 0: ...

If you want to keep a running count, you can use .count, which will 
count the number of substrings in the line.

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   I would have liked to have seen Montana.
   -- Capt. Vasily Borodin



More information about the Python-list mailing list