Character Classes in Perl

What is Character Classes in Perl?

Explanation

Character Classes includes a list of characters included in a square brackets. Matching of any of these characters result in a match of the character class. The characters inside the bracket with the combination of characters outside the brackets are matched for in the string.

Example :


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
@word=qw(joel joe john pol doe);
foreach (@word){
if (/[jpd]oe/)
{
print "Matched ! $_n";
}
else
{
print "Sorry.
$_n";
}
}
Result :

Matched ! joel Matched ! joe Sorry.john Sorry. pol Matched ! doe

In the above example a list is seperated by space as it is prefixed by the "quote words" its split by words, then in a loop each word is checked to have a pattern "joe" "poe" "doe", because the j,p,d are character classes any of which along with "oe" is matched for in the string.

Ask Questions

Ask Question