Regex search and replace with groups

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Regex search and replace with groups

Davide Varvello
Hi there,
 I'd like to remove a slash from a string like this one: 'abc123/456def/ghi'
The resulting string should be 'abc123456def/ghi' that is only slashes surrounded by numbers should be removed.

I tried with regex and group, using something like:

'abc123/456def/ghi'copyWithRegex: '(\d)/(\d)' matchesReplacedWith: '$1$2'

but of course it doesn't work, dollar sign doesn't identify the group, sigh.
Have you any suggestion?

Cheers
 Davide
Reply | Threaded
Open this post in threaded view
|

Re: Regex search and replace with groups

Davide Varvello
Any regex guru? :-)
Davide
Reply | Threaded
Open this post in threaded view
|

Re: Regex search and replace with groups

Peter H. Meadows
How about:

 'abc123/456def/ghi'copyWithRegex: '(\d)/(\d)' matchesTranslatedUsing:
[ :each | each first asString , each third asString  ].

Not sure if that's the best way, but it seems to work.

On Thu, Oct 25, 2012 at 1:58 AM, Davide Varvello <[hidden email]> wrote:
> Any regex guru? :-)
> Davide
>
>
>
> --
> View this message in context: http://forum.world.st/Regex-search-and-replace-with-groups-tp4652801p4652877.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|

Re: Regex search and replace with groups

Davide Varvello
:-)

yep, Peter, it works. I was looking for something semantically related to grouping, but your solution do the stuff
Thanks
 Davide


Peter H. Meadows wrote
How about:

 'abc123/456def/ghi'copyWithRegex: '(\d)/(\d)' matchesTranslatedUsing:
[ :each | each first asString , each third asString  ].

Not sure if that's the best way, but it seems to work.