Regex for beginners: multiple replacements

I have a line:

$mystring = "My cat likes to eat tomatoes.";

I want to make two replacements on this line using regex. I want to do s/cat/dog/and s/tomatoes/pasta/. However, I do not know how to properly format a regular expression to perform multiple replacements in the same expression on the same line in the same declaration. Right now, all I have is:

$mystring =~ s/cat/dog/ig;
$mystring =~ s/tomatoes/pasta/ig;
+3
source share
6 answers

My suggestion is you do it

my $text               =  'My cat likes to eat tomatoes.';
my ( $format = $text ) =~ s/\b(cat|tomatoes)\b/%s/g;

Then you can simply do this:

my $new_sentence = sprintf( $format, 'dog', 'pasta' ); 

Moreover:

$new_sentence    = sprintf( $format, 'tiger', 'asparagus' );

I'm going with the rest. You should not do all this in one expression or in one line ... but here is a way:

$text =~ s/\b(cat|tomatoes)\b/ ${{ qw<cat dog tomatoes pasta> }}{$1} /ge;
+2
source

, , , :

#!/usr/bin/perl

use strict;
use warnings;

use Regex::PreSuf;

my %repl = (
    cat => 'dog',
    tomatoes => 'pasta',
);

my $string = "My cat likes to eat tomatoes.";
my $re = presuf( keys %repl );

$string =~ s/($re)/$repl{$1}/ig;

print $string, "\n";

:

C:\Temp> t
My dog likes to eat pasta.

:

for my $k ( keys %repl ) {
    $string =~ s/\Q$k/$repl{$k}/ig;
}
+15

?

, Perl-ers , , (. - ), . /p >

, , .

EDIT:

, 5 , , - - . , .

+6

, perl @Sinan Ünür ( 123 eq '\d+' ).

Regexp::Assemble, :

use strict;
use warnings;
use Regexp::Assemble;

my %replace = (
    'cat' => 'dog',
    '(?:tom|pot)atoes' => 'pasta',
);
my $re = Regexp::Assemble->new->track(1)->add(keys %replace);

my $str = 'My cat likes to eat tomatoes.';
while (my $m = $re->match($str)) {
    $str =~ s/$m/$replace{$m}/;
}
print $str, $/;

$str = 'My cat likes to eat potatoes.';
while (my $m = $re->match($str)) {
    $str =~ s/$m/$replace{$m}/;
}
print $str, $/;

My dog likes to eat pasta.

+3

. "" "", " "

$mystring =~ s/(.*)cat(.*)tomatoes(.*)/$1dog$2pasta$3/g;
+1

, :

word => replacement

, '|'. (, "cat" "catogan" ), , sort reverse . (, "cat ++" ).

Regexp::Assemble . , .

, .

, , :

#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 6;

use Regexp::Assemble;

my $mystring = "My cat likes to eat tomatoes.";
my $expected = "My dog likes to eat pasta.";

my $repl;

# simple case
$repl= { 'cat' => 'dog', 'tomatoes' => 'pasta', };

is( 
    repl_simple($mystring, $repl), 
    $expected, 
    'look Ma, no module (simple)'
);  

my $re= regexp_assemble($repl);
is( 
    repl_assemble($mystring, $re), 
    $expected, 
    'with Regex::Assemble (simple)'
);

# words overlap
$mystring = "My cat (catogan) likes to eat tomatoes.";
$expected = "My dog (doggie) likes to eat pasta.";

$repl= {'cat' => 'dog', 'tomatoes' => 'pasta', 'catogan'  => 'doggie', };

is( 
    repl_simple($mystring, $repl), 
    $expected, 
    'no module, words overlap'
);  

$re= regexp_assemble( $repl);
is( 
     repl_assemble($mystring, $re), 
     $expected, 
     'with Regex::Assemble, words overlap'
);


# words to replace include meta-characters
$mystring = "My cat (felines++) likes to eat tomatoes.";
$expected = "My dog (wolves--) likes to eat pasta.";

$repl= {'cat' => 'dog', 'tomatoes' => 'pasta', 'felines++' => 'wolves--', };

is( 
    repl_simple($mystring, $repl), 
    $expected, 
    'no module, meta-characters in expression'
);  

$re= regexp_assemble( $repl);
is( 
    repl_assemble($mystring, $re), 
    $expected, 
    'with Regex::Assemble, meta-characters in expression'
);

sub repl_simple { 
    my( $string, $repl)= @_;
    my $alternative= join( '|', reverse sort keys %$repl);
    $string=~ s{($alternative)}{$repl->{$1}}ig;
    return $string;
  }


sub regexp_assemble { 
    my( $repl)= @_;
    my $ra = Regexp::Assemble->new;
    foreach my $alt (keys %$repl)
      { $ra->add( '\Q' . $alt . '\E'); }
    return $ra->re;
  } 

sub repl_assemble { 
    my( $string, $re)= @_;
    $string=~ s{($re)}{$repl->{$1}}ig;
    return $string;
  }
+1
source

Source: https://habr.com/ru/post/1718534/


All Articles