alpha Character Class in Regular expression

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

Explanation

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

PHP Example:


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

1213XXX4232

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!";}
else
{print "No Match!";}
Result :

Matches!

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