Last update:   29-10-2021


The rules change if your you're in a character class or not. For example, * is never a metacharacter within a class, while - usually is.
$text = "Is the star * a metacharacter?";

$text =~ s/([a-z *])+/.../g;

print $text; #Outputs: I...? 
Successful    (2/6)


A character class must always match a character to be successful.
$text = "the star * is a metacharacter";

if ($text =~ m/[A-Z]+/) {
    print "match";

#Outputs: nothing
Negated     (3/6)


A negated class must still match a character, but one not listed.
$text = "the star * is a metacharacter";

if ($text =~ m/[^a-z *]+/) {
    print "match"; #match at least one character not listed
#Outputs: nothing
Range Error    (4/6)

Range error

A range like [a-Z] is likely an error, it isn't "alphabetics".
$text = "a string";

if ($text =~ m/[a-Z]+/) {
    print "match";
#Outputs: Invalid range "a-Z"
Dot    (5/6)


Dot ussualy does not match a newline, while a negated class like [^*] ussualy does.
$text = "This is a 'quote \n on two' lines";
$text =~ /'(.*)'/;
print $1 . $2 . "\n"; #Output: nothing
$text = "This is a 'quote \n on two' lines";
$text =~ /'([^']*)'/;
print $1 . $2 . "\n"; #Output: quote \n on two
Posix Character Classes    (6/6)

POSIX character classes

An example is [:lower:], which represents any lower-case letter within the current locale. It is comparable with [a-z], but includes other characters such as ñ or õ. [:alnum:] alphabetic characters and numeric character [:alpha:] alphabetic characters [:lower:] lowercase alphabetics [:upper:] uppercase alphabetics
$text = "This is a 123 string";

if ($text =~ m/[[:alnum:]]/) {
    print "match";
#Output: match

Mode modifier

        A B C D E F