I need to apply a git word diff that treats every character as a word except when the characters are surrounded by square brackets. In that case, I want everything in the square brackets (even whitespace), including the brackets themselves and what they replace to be considered whole words so that none of these things are ever broken up.
Per the docs, "--word-diff-regex=.
will treat each character as a word [...]". So far so good, but I haven't been able to combine this with my other requirement of never breaking up brackets etc.
Here's a screenshot of the problem:
Since the letter 'e' appears both in the words 'The' and 'future', it's being reused and not marked as a change. I believe this is a result of using the regular expression .
. At the end, however, where only the letter 's' in 'possibilities' is highlighted as opposed to the entire word being replaced, that regular expression does the desired job.
The whole thing should look like this:
Here, both the brackets and what they replace are being treated as cohesive units, and the word 'possibilities' at the end is still not, which is perfect.
I have played with various regular expressions, including using XOR, to no avail. My guess is that, in many of my attempts, .
'swallows' whatever limiting expression I combine it with. I have gotten the specific example above to work with [a-zA-Z0-9\[]]|\s|\\n|[^[:space:]]
, but I don't know why (regex noob) and it doesn't work in other scenarios.
Keep in mind that git uses "POSIX “extended” regular expressions and not PCRE or Perl Compatible Regular Expressions, e.g. [:space:]
instead of \s
for the whitespace character class."