Dennie
asked on
python find position of string in entire file
Hi,
Below is a very simplified situation of my code. I'm iterating over lines in a file and on every line a regex is tried. I want the exact start position in the entire file if the regex matches. However the code below doesn't get the correct position. Any ideas? Please note, my situation doesn't allow to use a global regex on the entire file! I have to iterate over the lines.
Below is a very simplified situation of my code. I'm iterating over lines in a file and on every line a regex is tried. I want the exact start position in the entire file if the regex matches. However the code below doesn't get the correct position. Any ideas? Please note, my situation doesn't allow to use a global regex on the entire file! I have to iterate over the lines.
file = open('file.txt').read()
pos = 0
match_pos = 0
for line in file.splitlines():
match = re.search('function [^{]+?{', line)
if match:
match_pos = pos + match.start() #exact pos
print match_pos
print file.count('\n', 0, match_pos) #lineno
pos = pos + len(line)
ASKER
with exact position I mean that if I would use:
m = re.finditer('searchsomethi ng', file, flags=re.DOTALL)
m.start()
That the m.start in this code would match the start position of a match in the code of my first post (where i'm iterating over the lines)
Again, I have to iterate over all the lines!
m = re.finditer('searchsomethi
m.start()
That the m.start in this code would match the start position of a match in the code of my first post (where i'm iterating over the lines)
Again, I have to iterate over all the lines!
Are you looking for the offset from the beginning of the file (in bytes)?
Or are you looking for the offset at the beginning of each line?
Sorry for being confused.
The snippet of code that I supplied above displays the "exact" offset of the match, both the staring and ending position.
Or are you looking for the offset at the beginning of each line?
Sorry for being confused.
The snippet of code that I supplied above displays the "exact" offset of the match, both the staring and ending position.
ASKER
"The snippet of code that I supplied above displays the "exact" offset of the match, both the staring and ending position."
Yes but I have to iterate over the lines. your example is the exact match that I'm looking for, but I can't use a global regex in the file. I can only use a regex in the line.
Yes but I have to iterate over the lines. your example is the exact match that I'm looking for, but I can't use a global regex in the file. I can only use a regex in the line.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks for the assist, and the points.
Good luck & have a great day.
Good luck & have a great day.
Might you be getting into trouble with your calculations because of the "newline" characters a the end of each line?
Might something like this be what you want?
Open in new window