Python difflib with regular expressions
Can I use regular expressions in difflib?
Specifically, I'd like to do:
difflib.context_diff(ac开发者_开发技巧tual, gold)
Where actual is:
[master 92a406f] file modified
and gold is:
\[master \w{7}\] file modified
It looks like you mean that you want to ignore the 92a406f
part of the actual file. You should write a scrubber that uses regexes to scrub the parts you want to ignore:
actual = re.sub(r"\[master \w{7}\]", "[master *******]", actual)
then store the scrubbed gold file. Then you can use standard difflib to compare the scrubbed actual to the scrubbed gold.
If you really want to pursue a regex-based diff, then you can create your own string-like object that defines __eq__
based on regex matching, and use difflib on a sequence of those objects. I wouldn't recommend it, though.
What I just did is: replace the find_longest_match function of difflib with a copy, but replace the == invocations by invocation of a check that when things are not equal try to interpret the left side as regexp (and returns true on any error, e.g. when it is not a valid regexp).
I am using it for unit tests expected output matching and so far it is working really fine.
精彩评论