Hi:
I want to reach through a batch file that has many lines of which I want to do further processing only if there is a line starting with "call ..." Also the line should not contain
call %_anyvar%
How do I search for a pattern where it matches call in the beginning followed by any number of spaces and any word but not starting with %.
Pls help me out

import re
import os
def checkkfile():
fileIN = open(filename,"r")
for line in fileIN:
#line can be call %_junk% text1
#line can be call GetFiles %Label%

if (re.match(pattern,line)):
print ' '
else:
#more processing
checkfile()

Regards,
Indu

Recommended Answers

All 3 Replies

You can use the string module and the find() function (read all about it in the python reference ):
http://docs.python.org/lib/node42.html

line.find('text_I_want_to_find')

will normally give you the index of the found pattern, or else -1....I mostly use it to test the existence when parsing with "if line.find('blabla') !=-1:" then I haven't found that blabla pattern and I do certain things....

Helpful?

You can use the split() method.
substrs=line.split()
if (substrs[0]=="call") and (substrs[1].startswith("%"):
You will probably have to check for a length of 2 or more and may want to convert to lower case before comparing to "call", but that is the general idea.

The other solutions offered work fine. If you want to use a regular expression, the following may work also:

import re
patt = re.compile(r'call +(?!%\S+%)')

print patt.match('call %_junk% text1')
print patt.match('call GetFiles %Label%')

This matches the word 'call' at the beginning of the line followed by any number of spaces if not followed by a series on non-whitespace characters preceded and followed by the character '%'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.