Reputation: 31
I have a piece of code which extracts a string that lies between two strings.However,this script performs this operation only on a line.I want to perform this operation on a complete file and get a list of all the words lying between those two words.
Note:The two words are fixed.For ex:If my code is something like
'const int variablename=1'
then I want a list of all the words in the file lying between 'int'
and '='
.
Here is the current script:
s='const int variablename = 1'
k=s[s.find('int')+4:s.find('=')]
print k
Upvotes: 1
Views: 1060
Reputation: 336088
If the file fits comfortably into memory, you can get this with a single regex call:
import re
regex = re.compile(
r"""(?x)
(?<= # Assert that the text before the current location is:
\b # word boundary
int # "int"
\s # whitespace
) # End of lookbehind
[^=]* # Match any number of characters except =
(?<!\s) # Assert that the previous character isn't whitespace.
(?= # Assert that the following text is:
\s* # optional whitespace
= # "="
) # end of lookahead""")
with open(filename) as fn:
text = fn.read()
matches = regex.findall(text)
If there can be only one word between int
and =
, then the regex is a bit more simple:
regex = re.compile(
r"""(?x)
(?<= # Assert that the text before the current location is:
\b # word boundary
int # "int"
\s # whitespace
) # End of lookbehind
[^=\s]* # Match any number of characters except = or space
(?= # Assert that the following text is:
\s* # optional whitespace
= # "="
) # end of lookahead""")
Upvotes: 3
Reputation: 10278
I would use regular expressions over whole text (you can do it over one line too). This prints strings betweet "int " and "="
import re
text = open('example.txt').read()
print re.findall('(?<=int\s).*?(?=\=)', text)
Upvotes: 1
Reputation: 3727
If you want a quick and dirty ways and you're on a unix-like system.
I just should use a grep on the file. Then i will split the string in order to recognize the pattern and the data i want.
Upvotes: 0
Reputation: 2121
with open(filename) as fn:
for row in fn:
# do something with the row?
Upvotes: 2