I'm trying to search and capture for a certain type of file names (e.g. /app.css
/main.js
) within another file (a log file) .
The regex I've constructed is this:
^\/([a-zA-Z0-9_-]+)[.](css|js)
I'm trying to get the first capture group, i.e file name without extension (app
main
etc from above example) and this is how I'm searching
haystack = '/main.js'
matches = re.finditer(pattern, haystack, re.MULTILINE)
it works fine and I'm able to get the captured groups. However if I do the same while reading a file, it doesn't work
pattern = r"'^\/([a-zA-Z0-9_-]+)[.](css|js)'"
for i, line in enumerate(open('log.txt', 'r')):
haystack = line.rstrip()
matches = re.finditer(pattern, haystack, re.MULTILINE)
the content of the log.txt
is something like this:
duis ut diam quam /app.css porttitor
app.css
main.js
purus sit (amet volutpat /main.js)
it doesn't match in any of the lines for the above file, even though it should have in all four line!