Reputation: 26179
I have a very specific regex request. I need to match strings
When only using the first and last criteria this regex seems to work fine:
^.*m_.*(?<!Shape)$
But when I added the middle criteria I was lost.
Upvotes: 0
Views: 100
Reputation: 56829
This can be achieved with normal string methods in Python (I add brackets for clarity):
("m_" in input) and ("phys_" not in input) and (not input.endswith("Shape"))
I interpret (always some characters after "m_") as a hint that "phys_" never appears in front of "m_", rather than allowing the case where "phys_" comes in front of "m_" to pass.
Upvotes: 0
Reputation: 2759
The regex you want is
^(?=.*m_)(?!.*phys_)(?!.*Shape$).*$
It will capture the entire string, and each condition is in it's own lookahead. You can test it and see a visualization of what's happening on www.debuggex.com.
Upvotes: 1
Reputation: 215029
import re
r = re.compile(r'^(?=.*m_)(?!.*m_.+phys_)(?!.+Shape$)')
print r.match("aaa")
print r.match("aaa m_ xx")
print r.match("aaa m_ xx Shape")
print r.match("aaa m_ xx phys_ foo")
Basically, the principle is:
^
(?= .* should be there)
(?! .* should not be there)
Upvotes: 2