Task:
- given: a list of images filenames
- todo: create a new list with filenames not containing the word "thumb" - i.e. only target the non-thumbnail images (with PIL - Python Imaging Library).
I've tried r".*(?!thumb).*"
but it failed.
I've found the solution (here on stackoverflow) to prepend a ^
to the regex and to put the .*
into the negative lookahead: r"^(?!.*thumb).*"
and this now works.
The thing is, I would like to understand why my first solution did not work but I don't.
Since regexes are complicated enough, I would really like to understand them.
What I do understand is that the ^
tells the parser that the following condition is to match at the beginning of the string. But doesn't the .*
in the (not working) first example also start at the beginning of the string?
I thought it would start at the beginning of the string and search through as many characters as it can before reaching "thumb". If so it would return a non-match.
Could someone please explain why r".*(?!thumb).*"
does not work but r"^(?!.*thumb).*"
does?
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…