Perl string replacement

I want to convert each letter in a sentence to a specific letter depending on whether it is a consonant or a vowel, where the vowels are AEIOU.

So, if I have a line

$string = 'Hello' 

I would like to see

 $string = 'CVCCV' 

As a result.

I know I can use:

 $string =~ s/A/V/ $string =~ s/B/C/ $string =~ s/C/C/ 

etc., to check and convert each letter separately, but, of course, there should be a more efficient way to do this.

+4
source share
3 answers

Use ... s/[bcdfghjklmnpqrstvwxyz]/C/gi and s/[aeiou]/V/gi . They are called character classes . The i flag makes the case insensitive.

+6
source

normalize case, then apply the transliteration operator:

 $string = lc $string; $string =~ tr/aeioua-z/VVVVVC/; 
+19
source
 s/([aeiou])|[az]/ defined $1 ? 'V' : 'C' /ieg 
0
source

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


All Articles