About us Privacy Disclaimer Contact us
FAQ Help Advertising Feedback
Home Sitemap Search Donate us

  Home > Computer & I'net > Matching - Patterns...

   Browse by title articles:
   What is hot:

Matching - Patterns

Matching - Intro

Matching - Grouping

Matching - Extras

Files - 1

Files - 2

Building Link Popularity
Setting up mail accounts in Outlook
The 3 Musts for Running Your Web Design Business Efficiently and Effectively
Arrays Intro
Prev articles1234 5 6Next articles



Matching - Patterns


Computer & I'net articlesMatching - Patterns

by David Stanley    



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 ^ $

 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 =~ /eat/;

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/;

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=~ /eat/;

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 =~ /eatB/;

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

$string =~ /BeatB/;

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".




-----------------

Article by David Stanley. Visit his site http://www.htmlite.com. Reprinted with permission.






Articles

Auto & Trucks

Business

Computer & I'net

  ·General

  ·Apache

  ·CSS

  ·Database

  ·Hardware

  ·HTML

  ·Javascript&DHTML

  ·Linux

  ·MySQL

  ·Operating System

Perl / CGI

  ·PHP

  ·Programming

  ·Publishing

  ·Search Engines

  ·Software Problems

  ·SSI

  ·Tips & Tricks

  ·Utilities

  ·Web Design

Family

Food & Drink

Gardening

Health

Other

Pets

Psychology

Spiritual

Travel

Women

 Calculators

Car depreciation




  Disclaimer | Privacy | Terms of useCopyright © 2004 Nice2know.com