Re: small regex

12 views
Skip to first unread message

Isaac Wingfield

unread,
Apr 22, 2013, 1:15:11 PM4/22/13
to textwr...@googlegroups.com
From time to time, I have a need to modify a file in the sort of ways that (I suppose) regular expressions could easily handle. The difficulty is, I don't do things like that often enough to be able to remember how to set up the regex without spending a lot of time referring to documentation of one sort or another and doing a lot of trial and error until I get it just right. Plus, in my case, each file is different, so there's no point to working things out and creating a script because I'll never use it again.

As a long-time BBEdit-Lite and TextWrangler user, I've found it a lot more efficient to just work my way through the file one step at the time, using TextWrangler's very powerful "Replace All" and "Process Lines Containing …" features, while watching the output to see if I'm heading in the right direction. A very useful trick is to temporarily replace some string in the file with something else: suppose you want to change some instances of "[" but not others. In most structured files, (which are likely the kind needing this treatment) the "[" you want to keep will be part of a different longer string of characters than the ones you don't. So replace the entire larger string with something like "QQZZ", go ahead and operate on the file, and then replace "QQZZ" with the original string.

I suppose there might be situations where regular expressions would work but the incremental method would not, but so far I've been able to deal with every file I've needed to alter. And on the rare occasions when I will be performing the same set of operations on multiple files (usually months apart), putting the sequence of commands into an AppleScript and using that to control TextWrangler is a perfectly reasonable solution that requires no "relearning" every time it is used.

Isaac
Reply all
Reply to author
Forward
0 new messages