Page 1 of 1

Grappling with an RE

Posted: Wed Feb 06, 2013 10:17 am
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

Posted: Wed Feb 06, 2013 10:59 am
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: (.*)\[([^]]+)\]$

Posted: Wed Feb 06, 2013 12:39 pm
by terrypin
Thanks very much, appreciate the fast reply. Exactly what I need!

--
Terry, East Grinstead, UK