find/replace space to hyphen at specific column location?

General questions about using TextPad

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

Post Reply
ckhalleran
Posts: 1
Joined: Sun May 26, 2024 2:43 pm

find/replace space to hyphen at specific column location?

Post by ckhalleran »

I've never used regular expressions in Textpad, but this might work for me? I hope to check a few column locations on each line (27, 30, 33, 36, 39 and 42). If there's a space at that location, I want to change it to a hyphen/dash. And I want to do it to the end of the file.

Here's a screen image from the report that I am starting with. So if there's not an asterisk at that position (if it's a space) I want to change it to a hyphen.

Thanks,
Chris
screenimage.jpg
screenimage.jpg (20.77 KiB) Viewed 236 times
User avatar
AmigoJack
Posts: 490
Joined: Sun Oct 30, 2016 4:28 pm
Location: グリーン ヒル ゾーン
Contact:

Re: find/replace space to hyphen at specific column location?

Post by AmigoJack »

You have to search for each column separately:
  1. Find:

    Code: Select all

    ^(.{26}) 
    (Note the trailing space after the closing curly bracket.)
    Explanation:
    • ^ = start of line
    • () = capture what's in between the parenthesis
    • . = any character
    • {26} = repeat previous match (that is "any character") 26 times
    • (space) = find literally a space
  2. Replace with:

    Code: Select all

    \1-
    The "\1" part re-inserts the same 26 characters. And then the minus.
  3. Button "Replace all"
For each column, use either 26, 29, 32, 35, 38 and 41 as "repeat previous match". It's one less than the column of interest because the position of the column is already the one where you want to only match the space.

Regular expression work in TextPad the same way as elsewhere. You can learn everything about it reading https://www.regular-expressions.info/ and experiment on everything with https://regex101.com/.
Last edited by AmigoJack on Mon May 27, 2024 8:03 am, edited 1 time in total.
Reason: stupid mistake
User avatar
bbadmin
Site Admin
Posts: 811
Joined: Mon Feb 17, 2003 8:54 pm
Contact:

Re: find/replace space to hyphen at specific column location?

Post by bbadmin »

That will replace everything that's matched (i.e. all characters up to the space) with "-". Everything but the space needs to be saved in a register so it can be inserted in the replacement expression.

Code: Select all

Find what: ^(.{26})\s
Replace with: $1-
Note that \s matches tabs and spaces, but I used it to make the space visible here.
User avatar
AmigoJack
Posts: 490
Joined: Sun Oct 30, 2016 4:28 pm
Location: グリーン ヒル ゾーン
Contact:

Re: find/replace space to hyphen at specific column location?

Post by AmigoJack »

Oh yes - I totally forgot that. Corrected my post.

Your "\s" however could match more than only a space - the literal space would fit more IMO because then it wouldn't match unexpected "spaces" (like linebreaks or non-ASCII spaces). Or this is actually wanted...
Post Reply