Grappling with an RE

General questions about using TextPad

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

Post Reply
terrypin
Posts: 174
Joined: Wed Jul 11, 2007 7:50 am

Grappling with an RE

Post by terrypin »

I'm having a bit of difficulty getting this one right and would appreciate some help please.

I want to replace lines like this

abc xyx etc [Text in square brackets]

with

Text in square brackets abc xyz etc

I can get fairly close with

Find: (.*)(\[[A-Za-z\\]+\]$)
Replace with: \2 \1

But so far I'm darned if I can remove the square brackets in the replaced string. I suppose I could simply apply two more steps to remove '[' and ']' respectively, but I'm sure it should be possible in one expression?

Note: The reason for the '\\' is that it's possible the bracketed string might contain '\' as well as 'simple' words. Ideally I'd like to allow for any special characters and numbers, but that's another complication I haven't tackled yet.

--
Terry, East Grinstead, UK
ak47wong
Posts: 703
Joined: Tue Aug 12, 2003 9:37 am
Location: Sydney, Australia

Post by ak47wong »

To fix your regular expression, move the second set of parentheses so that they don't enclose the square brackets:

Find what: (.*)\[([A-Za-z\\]+)\]$

To allow for any special characters and numbers in the square brackets, change the [A-Za-z\\] to [^]]. This matches any character other than ], rather than just upper- and lower-case letters and backslashes. So:

Find what: (.*)\[([^]]+)\]$
terrypin
Posts: 174
Joined: Wed Jul 11, 2007 7:50 am

Post by terrypin »

Thanks very much, appreciate the fast reply. Exactly what I need!

--
Terry, East Grinstead, UK
Post Reply