2

I have this array.

my @input = ("He walk+V3SG very fast.", "He study+V3SG hard.");

and I want to substitute 'walk+V3SG' and 'study+V3SG' to 'walks' and 'studies'.

Below is the script I wrote. I thought this should work but for some reason it is not working.

    foreach my $sent(@input){
    if ($sent =~ m/\Q+V3SG/){
        if ($sent =~ m/\Q[dlr]y+V3SG/){
            $sent =~ s/\Q[dlr]y+V3SG/ies/g;
        }
        if ($sent =~ m/\Q[s|x|sh|ch|o]+V3SG/){
            $sent =~ s/\Q[s|x|sh|ch|o]+V3SG/es/g;
        }
        else {$sent =~ s/\Q+V3SG/s/g}
    }
}

foreach my $sent(@input){
    print $sent;
    print "\n";
}

Can anyone tell me what is wrong with the script?

0

2 Answers 2

2

The \Q makes the rest of the regex match literally [dlr]y+V3SG. Moving it enables the character class to function properly:

s/[dlr]\Qy+V3SG/ies/g

or just escape the +:

s/[dlr]y\+V3SG/ies/g

After this change, you get, e.g:

He stuies hard.

To make sure the first letter is retained, you can use a capture or \K (since 5.10):

s/[dlr]\K\Qy+V3SG/ies/g

For the second regex, you're using the wrong brackets:

s/(s|x|sh|ch|o)\Q+V3SG/$1es/g
1
  • 1
    Also, they shouldn't be using an if/elsif/else at all. The sentence could contain all three forms.
    – ikegami
    Apr 26, 2016 at 12:25
0

You should keep \Q just before the literal. You are placing it before whole regex, so the whole regex is considered as literal and is not interpreted.

Second thing you should use \K wisely to substitute. Put it just after the part you don't want to substitute. for eg: s/[dlr]\Ky\Q+V3SG/ies/g makes study studies and it will not remove d or l or r from result.

Third thing [s|x|sh|ch|o] will not do what you think. It will match any character in s,x,h,|,c,o. The correct one should be (?:s|x|sh|ch|o).(?:...) is for non capturing group.

Finally, that shouldn't be an if/elsif/else at all. The sentence could contain all three forms.

Overall: It gives us:

#!/usr/bin/perl
use strict;
use warnings;

my @input = ("He walk+V3SG very fast.", "He study+V3SG hard.","He crush+V3SG hard.");

foreach (@input){
    if (m/\Q+V3SG/){
        s/[dlr]\Ky\Q+V3SG/ies/g;
        s/(?:s|x|sh|ch|o)\K\Q+V3SG/es/g;
        s/\Q+V3SG/s/g;
    }
}

foreach my $sent(@input){
    print $sent;
    print "\n";
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.