alpha Character Class in Regular expression

What is [[:alpha:]] Character Class in regex?


This character class represents only alphabetic characters that is from [a-zA-Z] in regular expression.

PHP Example:

$str = '1213AAA4232';
$rslt = ereg_replace("[[:alpha:]]","X",$str);
echo $rslt;
Result :


In the above example for regex the character class "[[:alpha:]]" is used along with "$" to match the string at the end for alphabetical characters alone, since its matched it returns "1".

Perl Example:

#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$str = "3333";
if ( $str =~ /[[:alpha:]]|[[:alnum:]]/)
{print "Matches!";}
{print "No Match!";}
Result :


In the above example for regex the string has all numeric characters, in the pattern we use the "OR" or "|" operator with two character classes [[:alpha:]], [[:alnum:]] so that either an alphabhet or a number is matched, so for the above string [[:alnum:]] matches.

Ask Questions

Ask Question