Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regexp substitution

Reply
Thread Tools

regexp substitution

 
 
Mike Solomon
Guest
Posts: n/a
 
      07-19-2004
I have written the following script to replaces items in a line that
are enclosed with braces with variables of the same name

use strict;

my $test = 'mike test {VAR1} hhhh {VAR2}';

my %vars = (VAR1 => 'mike1', VAR2 => 'mike2');

while ($test =~ /{/) {
$test =~ /({.*?})/;
my $new = $1;
$new =~ s/{|}//g;
$test =~ s/{.*?}/$vars{$new}/;
}

print "$test\n";

I am sure there is a better way of writing this and would appreciate
your comments

Thanks

Mike
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-19-2004
Mike Solomon wrote:
> I have written the following script to replaces items in a line
> that are enclosed with braces with variables of the same name
>
> use strict;
>
> my $test = 'mike test {VAR1} hhhh {VAR2}';
>
> my %vars = (VAR1 => 'mike1', VAR2 => 'mike2');
>
> while ($test =~ /{/) {
> $test =~ /({.*?})/;
> my $new = $1;
> $new =~ s/{|}//g;
> $test =~ s/{.*?}/$vars{$new}/;
> }
>
> print "$test\n";
>
> I am sure there is a better way of writing this and would
> appreciate your comments


You can use the /g modifier instead of a while loop.
No need to first capture and then remove the braces.
No need for a temporary variable.

$test =~ s/{(\w+)}/$vars{$1}/g;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      07-19-2004
Mike Solomon <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> I have written the following script to replaces items in a line that
> are enclosed with braces with variables of the same name
>
> use strict;
>
> my $test = 'mike test {VAR1} hhhh {VAR2}';
>
> my %vars = (VAR1 => 'mike1', VAR2 => 'mike2');


Using a hash in this way is standard in this situation. The loop below
is more complicated than it has to be.

> while ($test =~ /{/) {


There's no need to search for each "{" singly. Just try to match
the complete pattern. Also, you don't need to escape "{" if it
doesn't look like a "{n,m}" quantifier. You didn't in the following
line:

> $test =~ /({.*?})/;


Don't capture things only to get rid of them later. You don't want the
"{}", so don't capture them:

$test =~ /{(.*?)}/;

> my $new = $1;


If you want the capture in $new, write that

my ( $new) = $test =~ /{(.*?)}/;

> $new =~ s/{|}//g;


Alternations of single characters are better written as character
classes

$new =~ s/[{}]//g;

but deletions of single characters are best done by tr///:

$new =~ tr/{}//d;

However, you don't need that step at all if you don't capture the {}
in the first place.

> $test =~ s/{.*?}/$vars{$new}/;
> }
>
> print "$test\n";
>
> I am sure there is a better way of writing this and would appreciate
> your comments


It can all be done in a single global substitution:

$test =~ s/{(.*?)}/$vars{ $1}/g;

Anno
 
Reply With Quote
 
Mike Solomon
Guest
Posts: n/a
 
      07-19-2004
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel) wrote in message news:<cdg5rs$7qh$(E-Mail Removed)-Berlin.DE>...
> Mike Solomon <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > I have written the following script to replaces items in a line that
> > are enclosed with braces with variables of the same name
> >
> > use strict;
> >
> > my $test = 'mike test {VAR1} hhhh {VAR2}';
> >
> > my %vars = (VAR1 => 'mike1', VAR2 => 'mike2');

>
> Using a hash in this way is standard in this situation. The loop below
> is more complicated than it has to be.
>
> > while ($test =~ /{/) {

>
> There's no need to search for each "{" singly. Just try to match
> the complete pattern. Also, you don't need to escape "{" if it
> doesn't look like a "{n,m}" quantifier. You didn't in the following
> line:
>
> > $test =~ /({.*?})/;

>
> Don't capture things only to get rid of them later. You don't want the
> "{}", so don't capture them:
>
> $test =~ /{(.*?)}/;
>
> > my $new = $1;

>
> If you want the capture in $new, write that
>
> my ( $new) = $test =~ /{(.*?)}/;
>
> > $new =~ s/{|}//g;

>
> Alternations of single characters are better written as character
> classes
>
> $new =~ s/[{}]//g;
>
> but deletions of single characters are best done by tr///:
>
> $new =~ tr/{}//d;
>
> However, you don't need that step at all if you don't capture the {}
> in the first place.
>
> > $test =~ s/{.*?}/$vars{$new}/;
> > }
> >
> > print "$test\n";
> >
> > I am sure there is a better way of writing this and would appreciate
> > your comments

>
> It can all be done in a single global substitution:
>
> $test =~ s/{(.*?)}/$vars{ $1}/g;
>
> Anno


That's perfect thanks everyone
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      07-20-2004
Mike Solomon wrote:

> (E-Mail Removed)-berlin.de (Anno Siegel) wrote in message
> news:<cdg5rs$7qh$(E-Mail Removed)-Berlin.DE>...
>>
>> It can all be done in a single global substitution:
>>
>> $test =~ s/{(.*?)}/$vars{ $1}/g;

>
> That's perfect thanks everyone


And in case the contents in the braces is not present in the hash:

$test =~ s/{([^}]+)}/exists $vars{$1} ? $vars{$1} : $1/eg;


John
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
problem with newlines in regexp substitution Florian Schulze Python 1 02-23-2006 09:10 PM
regexp substitution - a lot of work! Lukas Holcik Python 6 06-18-2004 05:05 PM
Re: searching for regexp match/substitution functions... William Ahern C Programming 0 06-28-2003 10:45 PM
Re: searching for regexp match/substitution functions... Arthur J. O'Dwyer C Programming 0 06-28-2003 09:02 PM



Advertisments