Simple Regex is beating me.

General questions about using TextPad

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

Post Reply
Crispy
Posts: 7
Joined: Tue Feb 21, 2006 9:06 am

Simple Regex is beating me.

Post by Crispy »

Hi,

I seem to be missing a trick here. I have data that looks like this;

1204,Bill Cook, 1205,Dave Smith, 1208,Bill Cook, 1212,Sally Jones, 1211,Bill Cook, 1202,Dave Smith,

Which I want to get looking like this;

1204,Bill Cook
1205,Dave Smith
1208,Bill Cook etc..

I can search using “[a-z]+, “ which finds all the surnames and the following comma & space, but if I try to turn it into a Regex, I come unstuck.

I thought this might work;

Search: \([a-z]+\) \(, \)
Replace \1\n

But the Regex is invalid and I don’t understand why. Can someone advise as to where I’m going wrong?

Many thanks.
BenjaminB
Posts: 10
Joined: Tue Sep 12, 2006 3:09 pm

Post by BenjaminB »

Try this:

Search for
^([0-9]+,[^,]*), ?

Replace with
\1\n

Click "Find Next" repeatedly until you are done (Unfortunately I did not find a better way to do this)

EDIT: I just DID find a better way ;)
Just search for
([0-9]+,[^,]*), ?

(there is a whitespace before the "?", but be sure not to copy the one at the end of the line)
Replace with
\1\n

and click "Replace all"


You have to use Posix Syntax for this to work. (Configure->Preferences->Use POSIX regular expression syntax)
Crispy
Posts: 7
Joined: Tue Feb 21, 2006 9:06 am

Post by Crispy »

Cheers for that, a swift reply and did exactly as I needed.
Perfect! - Many thanks.

Chris
User avatar
SteveH
Posts: 327
Joined: Thu Apr 03, 2003 11:37 am
Location: Edinburgh, Scotland
Contact:

Post by SteveH »

You need to perform the following search and replace:

Find what:
, *([0-9]+),
Replace with:
\n\1,
With regular expression enabled and POSIX regular expression syntax enabled in the general preferences.

This works with a replace all and relies on their being a comma in front of each number to avoid the need to repeat the search.

Hope this helps.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Or even
Find what: ,-
Replace with: \n
(Replace the hyphen with a space.)
User avatar
SteveH
Posts: 327
Joined: Thu Apr 03, 2003 11:37 am
Location: Edinburgh, Scotland
Contact:

Post by SteveH »

Zen and the art of the Regex.

Genius is in not missing the bleedin' obvious
Crispy
Posts: 7
Joined: Tue Feb 21, 2006 9:06 am

Post by Crispy »

Cheers all, not only have I learnt a couple of different ways of doing the same thing, but also to think more about what I'm actually trying to achieve!
Post Reply