complex patterrn search

Reply

Join Date: Jun 2008
Posts: 1
Reputation: indu_shreenath is an unknown quantity at this point 
Solved Threads: 0
indu_shreenath indu_shreenath is offline Offline
Newbie Poster

complex patterrn search

 
0
  #1
Jun 18th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 63
Reputation: tzushky is an unknown quantity at this point 
Solved Threads: 4
tzushky's Avatar
tzushky tzushky is offline Offline
Junior Poster in Training

Re: complex patterrn search

 
0
  #2
Jun 18th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,000
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 283
woooee woooee is online now Online
Veteran Poster

Re: complex patterrn search

 
0
  #3
Jun 18th, 2008
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.
Last edited by woooee; Jun 18th, 2008 at 10:46 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: complex patterrn search

 
0
  #4
Jun 18th, 2008
The other solutions offered work fine. If you want to use a regular expression, the following may work also:
  1. import re
  2. patt = re.compile(r'call +(?!%\S+%)')
  3.  
  4. print patt.match('call %_junk% text1')
  5. 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 '%'.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC