fixed line length

General questions about using TextPad

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

Post Reply
obfusc88
Posts: 8
Joined: Mon Dec 18, 2006 8:50 pm

fixed line length

Post by obfusc88 »

How can I make RegEx insert a \n at the last space char before char 65?

I want to do something like word wrap with some long text paragraphs. So If I want a width of 65 or less, I need to repalce the space with \n.

I can insert \n at position 65, but it sometimes ends up in the middle of a word, so I need to backup to the previous space char.

If TextPad RegEx cannnot do this can "normal" RegEx do it? If yes what woould the Search and Replace patterns look like?

I sometimes use this site: http://www.gskinner.com/RegExr/ to test out RegEx, so I would like to use the patterns there where I can save them, and then modify as needed for TextPad.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Find what: (.{1,63})_ [Replace the underscore with a space]
Replace with: \1\n

[X] Regular expression

Replace All
This assumes you are using Posix regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
Why not use Edit | Reformat ?
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

As usual, ben_josephs has a quick and correct solution.

But when I tried it I found that it cannot handle the last word in the paragraphs. That's because there is no trailing space character at the end of the line. So I would suggest that before you run this, you replace "\n" with "_\n" (underscore is a space char). When done you can remove the space by replacing "_\n" with "\n".

This is what I used for testing:
This is some dummy text just to test how the words will look after we do some modifications. This is some dummy text just to test how the words will look after we do some modifications. This is some dummy text just to test how the words will look after we do some modifications.
Hope this was helpful.............good luck,
Bob
obfusc88
Posts: 8
Joined: Mon Dec 18, 2006 8:50 pm

Post by obfusc88 »

WOW, did not think TextPad RegEx could look back but you guys found another way. I will try this out in the next few days.

PS - I thought thtt Reformat would do the whole document, but with a RegEx I thought I could do different paragraphs selectively by highlighting them.

Also trying to learn more about RegEx
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Bob's quite right. Here's a solution that requires only one step:
Find what: (.{1,63})(_|$) [Replace the underscore with a space]
Replace with: \1\n
If any text is selected, Edit | Reformat and Edit | Split Wapped Lines act only on the selected text.
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

Thanks ben_josephs

I had tried this: (.{1,63})(_|\n) but that did not work. Did not think of $, but now that I see it, that makes more sense.

Learning from you every day.
Hope this was helpful.............good luck,
Bob
Post Reply