September 14th, 2007

Regular expressions for converting code-indentation spaces to tabs in TextPad

I’ve never been a fan of using tabs to indent my code; I prefer spaces. Writing code is an art form, and when you use tabs to indent, you can’t assume that it will still look pretty on someone else’s machine, or in another application, etc., since tab sizes are platform-dependent and–although usually user-definable–the default size (typically 8 spaces, I think) tends to be much larger than my own 2-4 space indentation style.

However, some of my co-workers indent with tabs, and others indent with spaces. I’ve found that it’s easier for me to avoid inadvertently messing up existing code when I just bite the bullet and use tabs.

So, what follows are a couple of search/replace regular expressions I’ve recently used in TextPad, to make some existing code more consistent, by converting the spaces to tabs in certain relevant locations.


1. Regular expressions for aligning inline comments with tabs

Find what:   \;( *) {2}(\t*)\/\/
 
Replace with:   \;\1\t\2//

Customization: The number 2, in curly braces (above), should be replaced with the number of spaces that are used for indentation, in the code you’re running this on. In this case, the code was indented in increments of 2 spaces. Sometimes I deal with code that’s indented in increments of 4 spaces, in which case that 2 would change to a 4.

Manner of execution: Run via Replace All, repeatedly, until there are no more matches.

Recommended scope: I used it on a very specific block of selected code. Keep reading for the specific format.

When I used this on a block of code that consisted of variable declarations and //-style comments, it ended up making the comment blocks all line up nicely. Here is an animated GIF1 of a series of screenshots, showing how repeatedly running this expression transformed the comments into tab-indented comments that lined up nicely:

animated progression of screenshots showing comments separated from code with spaces, and eventually just tabs (and all lined up nicely)

I’m not sure how useful this will be, in general, because I think I just kind of lucked out when I got the results that I did. I thought it was worth sharing, though, because it impressed me when it produced results that were better than I had actually envisioned. :)

Two important things to note are:

  • The actual variable declarations (beginning of each line) were already indented with 1 tab.
  • There were a consistent number of spaces between the semicolons ending the variable declarations, and the double slashes starting the comments. While this didn’t help them line up, as spaces, it did make things pretty when the spaces were replaced with tabs.

If you’d like, you can see what my TextPad Replace dialog looked like, so you can see things like which checkboxes are checked, etc. Also, please note that in my TextPad preferences, I have it set to Use POSIX regular expression syntax (as previously mentioned, in another TextPad search/replace expression entry, a couple years ago).


2. Regular expressions for changing leading indentation spaces to tabs

Find what:   ^(\t*) {4}
 
Replace with:   \1\t

Customization: As with the first expression, above, the number in curly braces should be changed to the spacing increment used for indentation, in the code you’re running this on.

Manner of execution: Run via Replace All, repeatedly, until there are no more matches.

Recommended scope: It can be used on a block of selected text, the entire active document, or even on all open documents, if you’re brave and somewhat evil. :)


Footnotes

1 Sorry for using an animated GIF. I’d rather not have something constantly flashing on the page, because it distracts, and resembles an advertisement. I would have preferred another solution, but I racked my brain for over two hours, trying to decide how to present this series of 9 screenshots. I didn’t want to alienate readers who might read this using a feed reader that doesn’t support javascript, and I hate making people click through from the feed to the main site–I strongly prefer full-text rss feeds! Laying the 9 images out horizontally or vertically took up either too much space, or required making the images too tiny to read. In the end, this animated GIF seemed like the best portable way to show the effect I was trying to show. I won’t make a habit of using them, though. :)

[return to top] / [expression 1] / [expression 2]

October 23rd, 2006

MS Word’s Find and Replace has limited regex support

I just noticed a post on TGAW’s blog, Word 2003 - The Find What text contains a Pattern Match expression which is not valid, which contained some information that I don’t need right this second, but I do want to make a note of that’s more conspicious to me in the future…

She was trying to do a search/replace in Microsoft Word 2003, and stumbled across the fact that the Find and Replace dialog, with the ‘Use Wildcards’ option checked, “is pretty much a regular expression search (it does appear to be missing some support like \d, \w, etc)”.

I’m a big fan of regular expressions (although I must admit having been intimidated by them for years before finally getting comfortable using them), and often post code samples that used regexes, and just plain search/replace expressions that have proven useful to me, on my old geek blog (and in the future they will go here instead).

July 24th, 2006

People I would like to make contact with

This is a short list of various people I’d like to make contact with for one reason or another. These include former co-workers, people I went to school with, etc. I haven’t talked to any of these people in at least 4 years, and some it’s been as long as ~23 years. This is just a wild stab in the dark, in case one of these people happens to Google their name some day, and stumbles across this blog post. Highly unlikely, but it would sure be cool if it happened.

If your name is on this list, please consider that I’ve always lived in Northern Virginia, so if you’ve never lived or worked in that area, it’s not you that I’m looking for. If you think it might be you that I’m looking for, please email me!

Stephen O’Brien
Geoffrey Plumbley
Keith Landsdowne
Ba-Huy Duong
Lance Deffenbaugh
Eric Schmitt
Greg Highsmith
Jeremy Turner

July 15th, 2006

finding unique ips in access log that have something in common

search for a string in access log, extract only ip address from matching line, sort the list of ip’s and remove duplicates, output the [shortened] list…

for ip in `grep -i firefox /cygdrive/w/resin/log/access.log \
| grep -o “^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}” \
| sort -u \
| wc -l`; \
do echo $ip; \
done

July 13th, 2006

Using ImageMagick and 4NT to find jpegs by quality level

Using ImageMagick and 4NT to find and modify all jpegs in the current dir who have quality level 94:

for %f in (*.jpg) do (echo %f
& identify -verbose %f | grep Quality
| grep -o “[0-9]*”)

iff “%@execstr[identify -verbose %f
| grep Quality | grep -o “[0-9][0-9]”]” == “94″
then & echo %f & endiff

for %f in (*.jpg) do
(iff “%@execstr[identify -verbose %f
| grep Quality | grep -o “[0-9][0-9]”]” == “94″
then & echo %f &
mogrify -strip -quality 84 %f & endiff)


Unrelated side note:
This is one of several posts that I will be making in the near future, which come directly from my ever-growing toblog.txt file… No code cleanup or lengthly explanations, just commands I’ve run at some point that I thought were worth saving.