regexp substitution

Discussion in 'Perl Misc' started by Mike Solomon, Jul 19, 2004.

  1. Mike Solomon

    Mike Solomon Guest

    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
    Mike Solomon, Jul 19, 2004
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Jul 19, 2004
    #2
    1. Advertising

  3. Mike Solomon

    Anno Siegel Guest

    Mike Solomon <> 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
    Anno Siegel, Jul 19, 2004
    #3
  4. Mike Solomon

    Mike Solomon Guest

    -berlin.de (Anno Siegel) wrote in message news:<cdg5rs$7qh$-Berlin.DE>...
    > Mike Solomon <> 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
    Mike Solomon, Jul 19, 2004
    #4
  5. Mike Solomon wrote:

    > -berlin.de (Anno Siegel) wrote in message
    > news:<cdg5rs$7qh$-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
    John W. Krahn, Jul 20, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arthur J. O'Dwyer

    Re: searching for regexp match/substitution functions...

    Arthur J. O'Dwyer, Jun 28, 2003, in forum: C Programming
    Replies:
    0
    Views:
    699
    Arthur J. O'Dwyer
    Jun 28, 2003
  2. William Ahern
    Replies:
    0
    Views:
    706
    William Ahern
    Jun 28, 2003
  3. Lukas Holcik

    regexp substitution - a lot of work!

    Lukas Holcik, Jun 16, 2004, in forum: Python
    Replies:
    6
    Views:
    341
    Fredrik Lundh
    Jun 18, 2004
  4. Florian Schulze

    problem with newlines in regexp substitution

    Florian Schulze, Feb 23, 2006, in forum: Python
    Replies:
    1
    Views:
    266
    James Stroud
    Feb 23, 2006
  5. Joao Silva
    Replies:
    16
    Views:
    343
    7stud --
    Aug 21, 2009
Loading...

Share This Page