Stripping leading/trailing spaces, keep embedded spaces

General questions about using TextPad

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

Post Reply
scottbass
Posts: 17
Joined: Thu Nov 19, 2009 5:40 am
Location: Sydney, Australia

Stripping leading/trailing spaces, keep embedded spaces

Post by scottbass »

Hi,

I need to replace text as follows:

Code: Select all

This is a test$
     Another test$
          Still    Another    Test     $
To

"This is a test"
"Another test"
"Still Another Test"

IOW, I need to strip leading and trailing spaces, retain embedded spaces, and add double quotes to the text.

I've got an application called RegexBuddy, and in it I built this regex:

^(\s*)(.*?)(\s*)$ with replacement text "\2".

This works fine in RegexBuddy, but fails in TextPad. However, there seem to be many "flavors" of regular expressions, so perhaps I don't have RegexBuddy configured the same way as Textpad.

Questions:

1) Does Textpad honor the "?" modifier to make a regex "non-greedy"?

2) How can I accomplish the above requirement?

Thanks,
Scott
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

No, TextPad's regex engine is old and tired, and it doesn't support non-greedy repetitions. And it doesn't recognise the notation \s as representing a white space character.

But you can do what you want like this:
Find what: ^ *(.*[^ ]) *$
Replace with: "\1"

[X] Regular expression

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

[X] Use POSIX regular expression syntax
scottbass
Posts: 17
Joined: Thu Nov 19, 2009 5:40 am
Location: Sydney, Australia

Post by scottbass »

Hi, with my sample text

Code: Select all

This is a test   $
   This is a test$
   	This is a test   $
This worked for the first two lines, but not the third. Here is the results:

Code: Select all

"This is a test"
"This is a test"
"	This is a test"
I also tried

^( *)(.*[^ ])( *)$ replaced with "\2", same results.

Any idea if an upgrade to the regular expression engine is in the pipeline?

Thanks,
Scott
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Are there any tabs there?

Try
^[ \t]*(.*[^ ]) *$
or even
^[ \t]*(.*[^ \t])[ \t]*$
scottbass
Posts: 17
Joined: Thu Nov 19, 2009 5:40 am
Location: Sydney, Australia

Post by scottbass »

Wow Ben, you're good :) Thus the 1484 posts!

Yeah, my "code" document class converts tabs to spaces. But not the default document class, which is where I was doing my testing.

Thanks for the help, much appreciated.
Post Reply