Page 1 of 1

fixed line length

Posted: Thu Jul 23, 2009 10:28 pm
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.

Posted: Thu Jul 23, 2009 11:14 pm
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 ?

Posted: Fri Jul 24, 2009 12:20 am
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.

Posted: Fri Jul 24, 2009 1:04 am
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

Posted: Fri Jul 24, 2009 8:11 am
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.

Posted: Fri Jul 24, 2009 1:30 pm
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.