minte9
LearnRemember



Classes

The rules change if your you're in a character class or not.
 
#!/bin/perl -l
=begin

The rules change if your you're in a character class or not
Star (*) is never a metacharacter within a class, while
dash (-) usually is 

A character class must always match a character to be successful
A negated class must still match a character, but one not listed

Dot (.) ussualy does not match a newline, 
while a negated class like [^*] ussualy does

=cut

$a = "Is star * a metacharacter?";
$b = "star * is a metacharacter";
$c = "star * is a metacharacter!";
$d = "This is a 'quote \n on two' lines";

print ($a =~ m/([a-z *])+/);    # r
print ($b =~ m/[A-Z]+/);        # 
print ($c =~ m/([^a-z *])+/);   # !
print ($d =~ m/'(.*)'/);        # 
print ($d =~ m/'([^']*)'/);     # $1 = quote, $2 = on two

POSIX

POSIX expressions are a special kind of character classes.
 
#!/bin/perl -l
=begin

POSIX bracket expressions are a special kind of character classes
They use the same syntax with square brackets

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

=cut

$a = "This is a 123 string õ";

print ($a =~ m/([[:alnum:]]+)/); # This
print ($a =~ m/([[:alnum:]]+)/g); # Thisisa123string



  Last update: 186 days ago