blank Character Class in Regular expression

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

Explanation

The [[:blank:]] represents horizontal space, tab-space characters in regular expression.

PHP Example:


<?php
$str = 'A12 34ax$ xxxx';
$ret = ereg_replace("[[:upper:]]|[[:blank:]]","B",$str);
echo $ret;
?>
Result :

B12B34ax$Bxxxx

In the above example for regex the character class "[[:blank:]]" is used along with "[[:upper:]]" to match a string that is in uppercase and having one or more tab or blank-space.

Perl Example:


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$str = "A A AA";
if ( $str =~ /[[:blank:]]{1,2}/ )
{print "Matches!";}
else
{print "Unmatch!";}
Result :

Matches!

In the above example for regex the string "A A AA" has a two blank-spaces, so does tha pattern "[[:blank:]] {1,2}" which specifies number of blank-spaces minimum "1" space and utmost "2" blank-spaces.

Ask Questions

Ask Question