On 8 Apr 2014, at 1:34, Matt Neuburg wrote:
Find expression:
(?<orig>hello)
Replace expression:
${orig/(?<one>hey)?/${one:?howdy:scram}/}
[…] I expect _all_ of "hello" to be replaced by "scram". In other words, I expect the replace expression to be a calculation about what to put in place of the whole text found by the Find expression.
The second assertion here is correct. You ask TextMate to find “hello” and replace it.
But your replacement text is effectively “$orig”, i.e. the original string found.
What you do is a substitution on the “$orig” variable, but this substitution is “replace ‘hey’ with ‘howdy’, and if you do not find ‘hey’, insert ‘scram’”.
The problem is “not finding hey” expressed using “(hey)?” means that zero bytes is considered “not finding ‘hey’”.
What you probably want is (I switched back to numbered captures for readability):
${orig/(hey)|.+/${1:?howdy:scram}/}
What this regular expression says (‘(hey)|.+’) is “find ‘hey’ and put in capture $1 OR (‘|’) find anything (‘.+’)”.
So when it gets time to evalulate what to insert for what we found (‘${1:?howdy:scram}’) then we either matched ‘hey’ and will repalce that with ‘howdy’ or we replaced non-zero bytes of something else, and will replace that with ‘scram’.