shadowline
Matching - Patterns << Matching - Grouping >> Matching - Extras
Square brackets [] are used to group together a series of characters. A dash - is used to symbolize a "between" set of numbers or letters. The up ^ symbol represents "not". Here are some examples on what may be seen and what they mean.

BracketsDescriptionAlternative
[0-9]All digits 0 to 9\d
[^0-9]No digits 0 to 9\D
[_a-zA-Z0-9]All characters, letters and numbers\w
[^_a-zA-Z0-9]No characters, letters or numbers\W
[\r\t\n\s]All returns, tabs, newlines, spaces\s
[^\r\t\n\s]No returns, tabs, newlines, spaces\S

The square bracket [ ] option is used to single out a list of options that will match. Think of an OR being in between each character inside the square brackets.

$mystring =~ /The year is 200[0123456789]/;

A true result will be found if the $mystring a value of "The year is 2000" up to "The year is 2009".

$mystring =~ /I am yelling[.!]/;

A true result will be found only if the sentence "I am yelling" is matched in the $mystring and it ends with either a period OR an exclamation mark.

Now if you're truly lazy like me, typing in a bunch of numbers or letters can get rather tiring. Lukily, Perl has given us a shortcut method!

[0123456789] equals [0-9]

And it works for letters too :

[abcdefghijklmnopqrstuvwxyz] equals [a-z]

How's that for a sweet deal ?!? You don't have to use all the characters or numbers either. Say you just want c to t it would look like [c-t] and so on.



As seen above, EXCLUSION is combining the ^ and square brackets [] to select all the characters you do not want to be part of a pattern.

$string =~ /200[^1-9]/;

This above example will return TRUE as long as the number value of $string does NOT end with a number from 1 to 9. In other words, the only TRUE result is if $string holds the number 2000. We excluded all the other numbers.



The letter alternatives become useful when we are doing a more general search.
$string1="The year is 2000 and it is going to be over soon.";
$string2="Next year will be 2001.";
   print "String 1 contains a value of $string1 \n";
   print "String 2 contains a value of $string2 \n";
   print "We are searching for four digits followed by a space character \n";
   if ($string1 =~ /\d\d\d\d\s/){ print "I made a match in string 1\n";}
      else{ print "No match in string1\n"; }
   if ($string2 =~ /\d\d\d\d\s/){ print "I made a match in string 2\n";}
      else{ print "No match in string2\n"; }

See the example

Looks kinda scary and complicated doesn't it? It's not really. The first part declared a couple variable values. The second part printed them out to the monitor. You've seen the IF ELSE statements before so that just leaves the actual search pattern part.

\d\d\d\d\s

The IF statement checks to see if the next statement holds a true or false value. A true value would print out the first IF part, a false value will print out the ELSE part. The statement is trying to find a match to... digit digit digit digit space. The first string holds true having the number 2000 within it. The second string holds false considering there is a period after the number (not a space).



Brackets can also hold the exact characters allowed as a bundle.

$string =~ /[Hh]ello [Ww]orld/;

The above example will allow either a capital or lowercase letter for the beginnings of the words to hold a true value. Hello World, Hello world, hello world, hello World.

That works for individual characters, but if you need to check for an EITHER OR of a full number or word, it's time to use the pipe | character.
$string1="Next year will be 2001";
$string2="2002 will be here before you know it";
   print "$string1\n";
   print "$string2\n";
   print "Searching for 2000 or 2001 in the strings. \n";
   if ($result = $string1 =~ /2000|2001/){print "match in string1\n";}
      else {print "no match in string1\n";}
   if ($result = $string2 =~ /2000|2001/){print "match in string2\n";}
      else {print "no match in string2\n";}

See the example

The expression holds true as long as the string contains either 2000 or 2001 inside of it.
Matching - Patterns << Matching - Grouping >> Matching - Extras
shadow

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads
shadow
shadow