Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 22nd, 2009, 05:32 PM
patrickgilmour@ patrickgilmour@ is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 17 patrickgilmour@ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 8 m 23 sec
Reputation Power: 0
Replace multiple characters with multiple characters

Hi,

I have various strings in a document of the type:

~,~
~,,,,,,~
~,,,~

And I need to replace them to get:
~x~
~xxxxxx~
~xxx~

The only criterion is that it only affects , characters that are between two ~

I've got (~)(,+)(~) to find all the comas I need.

I can get it to replace ~,~ with ~x~ but when there are multiple comas it only replacing with one ,

So ~,,,,,,~ becomes ~x~

Any idea how to get multiple replacements like this?

Many thanks,

Pat

Reply With Quote
  #2  
Old May 22nd, 2009, 05:59 PM
OmegaZero OmegaZero is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2007
Posts: 738 OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 8 m 21 sec
Reputation Power: 928
__________________
sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);

Reply With Quote
  #3  
Old May 24th, 2009, 10:00 AM
chorny_cpan chorny_cpan is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 159 chorny_cpan User rank is Captain (20000 - 30000 Reputation Level)chorny_cpan User rank is Captain (20000 - 30000 Reputation Level)chorny_cpan User rank is Captain (20000 - 30000 Reputation Level)chorny_cpan User rank is Captain (20000 - 30000 Reputation Level)chorny_cpan User rank is Captain (20000 - 30000 Reputation Level)chorny_cpan User rank is Captain (20000 - 30000 Reputation Level)chorny_cpan User rank is Captain (20000 - 30000 Reputation Level)chorny_cpan User rank is Captain (20000 - 30000 Reputation Level)chorny_cpan User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Day 1 h 51 m 20 sec
Reputation Power: 207
With tests:
Code:
#!/usr/bin/perl

use 5.006;
use strict;
use warnings;
use Test::More qw(no_plan);

#Replace all ',' in strings like ~,+~ with x
sub replace {
  my $str=shift;
  $str=~s/~(,+)~/'~'.('x' x length($1)).'~'/ge;
  return $str;
}

is(replace('~,~'),'~x~');
is(replace('~,,,,~'),'~xxxx~');
is(replace('~~ ,'),'~~ ,');


You should be able to convert this to any language that supports callbacks in regexes.

Reply With Quote
  #4  
Old May 24th, 2009, 03:27 PM
patrickgilmour@ patrickgilmour@ is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 17 patrickgilmour@ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 8 m 23 sec
Reputation Power: 0
Wow! Thanks for the help on this guys! Really appreciated.

P



Quote:
Originally Posted by chorny_cpan
With tests:
Code:
#!/usr/bin/perl

use 5.006;
use strict;
use warnings;
use Test::More qw(no_plan);

#Replace all ',' in strings like ~,+~ with x
sub replace {
  my $str=shift;
  $str=~s/~(,+)~/'~'.('x' x length($1)).'~'/ge;
  return $str;
}

is(replace('~,~'),'~x~');
is(replace('~,,,,~'),'~xxxx~');
is(replace('~~ ,'),'~~ ,');


You should be able to convert this to any language that supports callbacks in regexes.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Replace multiple characters with multiple characters

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap