Forms & Hashes << Matching - Intro >> Matching - Patterns
A regular expression is a group of characters you want to search for in a string. A pattern is a specific sequence of character(s) you may be searching for in a string.

MATCH operators will test a string of information to see if the specified regular expression is present. If true, the statement returns a true (non-zero) value. If no match, it returns a zero or false value. The match is symbolized by an equals and tilde.


To test for the opposite result (having the true bring a false value) you can use an exclamation point and tilde. It isn't a common practice, but it may come in handy to know someday.


Normally (I say that because there are situations where it may be different, but normally) the following will hold true : To specify a regular expression (a piece of data to use in some way), it can be entered within two forward slashes.

/somthing here/

Now that you have a regular expression, we can start comparing (matching) it to a string.

$mystring =~ /today/;

This will take the word today and look through the value of $mystring. If the match finds the word it will produce a true result. The item to search for doesn't have to be an actual word. It can be a variable as well such as...

$search_for = "today";
$mystring =~ /$search_for/;

There are a number of "special" characters to use when it comes to matching, searching, etc.. The first one to cover is the plus + option. This is used inside the regular expression area and is used to specify that the character before it may occur one or more times and still satisfy the search parameters.

$mystring =~ /to+day/;

Now the search will look for the word today which may contain one or more of the o character. So that means it will be true for today, tooday, toooooooday, and so forth.

The asterisk * option specifies that zero or more of the previous character can be present to make the match true. This is similar to the plus character, except the asterisks don't require one of the charcters to be present.

$mystring =~ /to*day/;

The search will look for the word today and will hold a true value even if the o is missing. So it will be true for today, tooday, toooooooday, and tday as well now. The plus feature mentioned earlier stated there must be at least one occurance. The asterisk takes it one step further and allows a non-occurance.

The question mark ? option specifies that zero or one of the previous character can be present to make the match true.

$mystring =~ /to?day/;

This time, the only true value will appear for the word today and tday. It is matching for exactly one or none of the previous character.

What if you want to specify the number of times a character is found? This is done using the curly bracket {} characters. They pre-specify how many times a certain character should appear in an expresssion.

$mystring =~ /to{3,6}day/;

There will be a true result if the o is repeated 3 to 6 times only. So that is toooday, tooooday, toooooday, tooooooday.

To specify only a maximum number of characters, a zero can be entered for the first digit.
$mystring =~ /to{0,6}day/;

To specify only a minimum number of characters, leave the second digit area blank.
$mystring =~ /to{3,}day/;

Or to specify an exact number of characters, enter only one number.
$mystring =~ /to{2}day/;

The period . option specifies that any character (other than newline \n) will match true.

$mystring =~ /a.t/;

This example will produce a true value if it contains the letters "a" and "t" with almost any character in-between. Some examples that would produce a true result is if $mystring was : a1t, act, a!t, factor, author.

Now using the information you have seen so far, here is a small quiz. How would you find a word that starts with the capital "T" and ends with the lower case "e" in the $mystring variable? Highlight the below space to find the answer!
$mystring =~ /T.+e/;
Note : If there is more than one "e" in the string after the "T" then the last "e" found in the string would be the one used in the search parameters.

There may come a time when you want to use one of the above "special" characters as part of the search pattern. How are you going to ask the script to search for a question mark when it is being used for a special command? By using a backslash \ character. This is known as an escape character. It states the next character is not really a separate command, but normal instead.

$mystring =~ /hello\?/;

So now the search will be able to look for "hello?".
Forms & Hashes << Matching - Intro >> Matching - Patterns

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads