Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Perl-Compatible Regular Expressions (PCRE)
 
Article Index
Perl-Compatible Regular Expressions (PCRE)
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Page 9
Page 10
 

/^(\w+) is a programmer. \1's specialty is programming.$/

If you try to match the preceding regex against the following sentence:

Marco is a programmer. Marco's specialty is programming.

Everything will work fine. However, if you try it against this sentence:

Marco is a programmer. John's specialty is programming.

The regex compiler will not return a match because the reference won't match.

To give you an idea of how powerful PCREs are and why it's worth trying to learn them, let me give you an alternative to the simple one-line expression using POSIX:

<?php

$s = 'Marco is a programmer. Marco\'s specialty is programming.';

if (ereg ('^([[:alpha:]]+) is a programmer', $s, $matches)) {
if (ereg ('([[:alpha:]]+)\'s specialty is programming., $s, $matches2)) {
if ($matches[1] === $matches[1]) {
echo "MATCH\n";
} else {
echo "NO MATCH\n";
} else {
echo "NO MATCH\n";
} else {
echo "NO MATCH\n";
}
?>

Now, this is a simple example, and the POSIX solution is definitely not as elegant as it could be, but you can see here that it takes three separate operations to approximate the power of just one PCRE.


Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Wallpaper 65
Statistic


Last Post

 
Top! Top!