Regular expression – replacing characters in Perl

I am trying to double all the vowels in each word. For example:

$string="if it rains, cover with umbrella";

This is the code I wrote, but I did not get the correct output.

$string=~s/a |e|i|o|u/aa|ee|ii|oo|uu/gi; print $string;

Expected output: iif iit raaiins cooveer with uumbreelaa

Someone Can you help me?

The regular expression in the replacement should work, but as you will see, the replacement A string is a simple string that has nothing to do with matching, unless you capture substrings in regular expressions and use them in replacement strings.

Use character classes to match a set of characters Any of them, such as [aeiuo].

Use parentheses to “capture” the matched part so that you can use it in the replacement string.

my $string = "if it rains, cover with umbrella";

$string =~ s/([aeiuo])/$1$1/g;

print $ string;

Yield

iif iit raaiins, cooveer wiith uumbreellaa

I am trying to All vowels in each word are doubled. For example:

$string="if it rains, cover with umbrella";

This I wrote the code, but I did not get the correct output.

$string=~s/a|e|i|o|u/aa|ee|ii| oo|uu/gi; print $string;

Expected output: iif iit raaiins cooveer with uumbreelaa

Can anyone help me?

The regular expression in the replacement should work, but as you will see, the replacement string is a simple string that has nothing to do with matching. Unless you capture substrings in regular expressions and use them in replacement strings.

Use character classes to match any one of a set of characters, such as [aeiuo].

p>

Use parentheses to “capture” the matched part so that you can use it in the replacement string.

my $string = "if it rains, cover with umbrella";

$string =~ s/([aeiuo])/$1$1/g;

print $string;

Yield p>

iif iit raaiins, cooveer wiith uumbreellaa

Leave a Comment

Your email address will not be published.