shadowline
Matching - Intro << Matching - Patterns >> Matching - Grouping
PATTERNS
What if you are looking for "eat" specifically but keep getting a true value from a word such as "feature"? Anchoring predetermines exactly where you want the pattern to be for it to match. There are 4 different ANCHOR types. b B ^ $



\b tells the regular expression that the next batch of characters must be on a word boundary. That is... the beginning or the end of a word. Here is an example showing how to search for the letters "eat" at the beginning of a word :

$string =~ /\beat/;

The above expression will be TRUE if the variable $string holds a value such as eating, eat, eaten, eat@joes, and so on. The expression will be FALSE with values such as feature, Eating, and Beat.

The string "feature" does contian "eat" but not at the beginning of the word. The string "Eating" contains "Eat" at the begining of a word but the E character is in uppercase and regualur expressions are case sensitive by default.

Here is an example showing how to search for the letters "eat" at the end of a word :

$string=~ /eat\b/;

An example of a TRUE result could be "Beat it, get out of here!". The search goes through each word of the string individually and checks for the letters "eat" on the ending of each word.
In addition to being able to search for a string and the beginning or the end of a word, you can search for a string at the beginning AND the end of the word. This is normally used to find a single specific word within a string and return a TRUE value.

$string=~ /\beat\b/;

The search looks for the word "eat" by itself, not within another word in the value of the $string variable.



\B is the opposite of the above. It will search for a string but it must be within a word, not on the start or ending of a string.

$string =~ /\Beat/;

An example of a TRUE result could be meat or feature. An example of a FALSE result would be eating or eatery. One or more characters MUST appear before the search element. To do a search where one or more characters must be AFTER the search element :

$string =~ /eat\B/;

And to do a search where one or more characters must be showing before AND after the search element :

$string =~ /\Beat\B/;




The b and B searches look at each word within a string. Using ^ will do a search at only the start of a string as a whole, not as individual words.

$string =~ /^if/;

A true result would happen if $string holds a value such as "if this is true.".

A false result would happen for a value such as "If this is true." and also "Only if it happens" The first false statement has a capital as the first letter. The second false statement has the search word not at the start of the string value.



The opposite to ^ is $. This will search at the ending of a string value.

$string =~ /if$/;

A true result would happen if $string holds a value such as "Only if".

A false result would happen for a value such as "Only If" and also "Only if it happens" The first false statement has a capital as the first letter. The second false statement has the search word not at the end of the string value.



Be careful about capital letters, lowercase letters and punctuations at the start and endings of strings when you are working with the ^ and $ patterns.

To find a one and only specific string, you can combine these two search anchors :

$string =~ /^if$/;

Now that search will only be true IF the value of $string is the single word "if".
Matching - Intro << Matching - Patterns >> Matching - Grouping
shadow

Advertise in the banner along the top, Click here!

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