[TxMt] Re: possible format strings bug

Matt Neuburg matt at tidbits.com
Fri Apr 4 16:31:34 UTC 2014


On Apr 4, 2014, at 7:58 AM, Allan Odgaard <mailinglist at textmate.org> wrote:

> On 4 Apr 2014, at 21:45, Matt Neuburg wrote:
> 
>> Find expression (regex):
>> 
>> ^(=+)
>> 
>> Replace expression:
>> 
>> ${1/=(=)?(=)?/${2:?2:${1:?1:0}}/}
>> 
>> Do a replace all […]
> 
> The problem is that your first search string ‘^(=+)’ populates capture register $1 and the nested replacements inherit variables from parent matches.

Thank you! Brilliantly explained. I understand completely (that's a bit scary, but never mind).

> In your case, you can lose the parentheses. If you needed captures in the root search and conditionals in a nested replacement, a workaround would be to use named captures (to avoid clashes).

As you have guessed, this is a simplification of a larger problem. In real life, I can't lose the parentheses because I need the group to assign it a scope (in the AsciiDoc language grammar); my "find expression" is actually considerably longer. Unfortunately one can't mix named groups with implicitly numbered groups, so it's going to be named groups all the way. Just for the record, this solves it:

Find expression:

^(?<first>=+)

Replace expression:

${first/^=(?<two>=)?(?<three>=)?$/${three:?2:${two:?1:0}}/}

Of course, in real life, it's all going to be quite a bit longer, so now I have to go see if I can write it without losing my mind. :)

m.

PS I based my expression on the parallel expression in the Markdown bundle - see repository > block > repository > heading. So now we know that the expression in the Markdown bundle is suffering from the same bug (in the expression, not in TextMate), but it does not surface because the default "1" is acceptable. In AsciiDoc, though, one equal sign is called a "level 0 heading".

--
matt neuburg, phd = http://www.apeth.net/matt/
pantes anthropoi tou eidenai oregontai phusei
Programming iOS 7! http://shop.oreilly.com/product/0636920031017.do
iOS 7 Fundamentals! http://shop.oreilly.com/product/0636920032465.do
RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html




More information about the textmate mailing list