Proper Treatment 正當作法/ blog/ posts/ The the/ discussion 討論
2008-08-17 19:19

In Perl I’d break this up into subregexes, each its own variable, then combine them into the final regex. This always makes it easier for me to read the thing months later when I’ve forgotten what problem I was trying to solve. Does vim allow regexes to be broken up in this way?

You can do that using Vim’s scripting language, but I wanted to make it something I can paste into a terminal window after hitting “/” for search.

Also, in Perl-compatible regular expressions, I thought you’d find a repeated word like so:

$string =~ m#(\w+)\s+\1#gsmi

You’d need to be cleverer about the \w+ bit, because that matches a programming language’s sense of what a ‘word’ is rather than a natural language’s sense of what it is.

That’s what I do too—the back-reference \1 does appear near the end of my pattern. I let the word include apostrophes (but not at the beginning or end), to find “don’t don’t”. I also force the word to begin and end at a boundary, to skip “important antic”.

Maybe I’m just not reading your regex properly.


Sometimes one needs to say something like ‘By the time he phoned me, I had had dinner already.’