Regex / macro to align columns

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
kkaran
Posts: 6
Joined: Thu May 01, 2008 4:54 pm
Location: New York City

Regex / macro to align columns

Post by kkaran »

Given 3 columns of whitespaced delimited words with uneven spacing, I would like to align the columns:

I have:

Code: Select all

xxxxxxxxx  AS  yyyyyyyyyyyyyyyy
x AS yyy
xxxx                  AS yy
I would like:

Code: Select all

xxxxxxxxx AS yyyyyyyyyyyyyyyy
x         AS yyy
xxxx      AS yy
I would like to use search and replace with regular expressions. The shortest way I could think of is to search and replace in two passes.:
First replace:
  • [[:blank:]]+
  • <blank repeated the number of times as long as the first string>
  • Regular expression box checked
Second replace:
  • ^(.{9})[[:s:]]+([^[:s:]]+)[[:s:]]+([^[:s:]]+)$
  • \1 \2 \3
  • Regular expression box checked
Problems I need help solving:
  1. For the first replace, instead of hitting the spacebar x times I would like to be able to specify the repeat number in code something like: <space>{9}
  2. For the first and second replace I would like to prompt the user for the length of the string instead of hard-coding the number (in this case 9).
  3. It would be great if the above two steps could be combined into one.
I have searched and found the following links which point to a Perl solution which is more complicated than I'd like. Suggestions appreciated. If I have not done my homework, please point me to where I should RTFM.
ak47wong
Posts: 703
Joined: Tue Aug 12, 2003 9:37 am
Location: Sydney, Australia

Post by ak47wong »

I don't think there's a practical way to align text using regular expressions (though I'm happy to be proven wrong), but would you consider using tabs to solve your problem? That is what they're for, after all.

Step 1: Align the "AS" keywords with tabs
Find what: _+AS_+ [replace the underscores with spaces]
Replace with: \tAS_ [replace the underscore with a space]

Step 2: Set the tab width
1. Click View > Document Properties.
2. Click Tabulation.
3. In Default tab spacing, enter the width of the longest "x" string plus 1 (in your example, 10).
4. Select Convert existing tabs to spaces when saving files.

Step 3: Convert the tabs to spaces
5. Save the file.
kkaran
Posts: 6
Joined: Thu May 01, 2008 4:54 pm
Location: New York City

Post by kkaran »

That is not the solution I was hoping for but it does work. You are correct that that is what tabs were meant for. I was hoping to be able to highlight a range, select a menu option pointing to a macro, and be prompted for a column number. Alas, that is not meant to be.

Thank you AK47 nevertheless.
Post Reply