| | |
complex patterrn search
![]() |
•
•
Join Date: Jun 2008
Posts: 1
Reputation:
Solved Threads: 0
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
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
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?
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?
•
•
Join Date: Dec 2006
Posts: 1,000
Reputation:
Solved Threads: 283
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.
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.
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
The other solutions offered work fine. If you want to use a regular expression, the following may work also: 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 '%'.
Python Syntax (Toggle Plain Text)
import re patt = re.compile(r'call +(?!%\S+%)') print patt.match('call %_junk% text1') print patt.match('call GetFiles %Label%')
![]() |
Other Threads in the Python Forum
- Previous Thread: Appending data in a word doc
- Next Thread: [Python]Create dynamically loadable APi
| Thread Tools | Search this Thread |
address alarm anydbm app beginner changecolor cipher clear conversion coordinates corners curves cx-freeze data definedlines development dictionary directory dynamic events excel feet file float format function generator getvalue halp handling homework images import input ip itunes java keycontrol line linux list lists loan loop maintain matching maze millimeter mouse number numbers output parsing path port prime programming py2exe pygame pymailer python queue random rational raw_input recursion recursive screensaverloopinactive script scrolledtext searchingfile slicenotation split ssh string strings tails text threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable variables ventrilo vigenere web webservice word wxpython xlwt






