in reply to regex and matching

The key to the secret is the ' and'.

m/(?<last_name>\w+) and \w+ \g{last_name}/

This regex matches the word (i.e. \w+ characters, this does not include whitespace!) just before the ' and' and saves it under <last_name>, and which is followed by another word and the same word as what was just saved under <last_name>.

The only fixed point in this regex is the ' and', so it is easiest to start by this fixed point and see how the regex tries to match from there.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: regex and matching
by hasnainzeenwala (Novice) on May 28, 2011 at 06:26 UTC
    Hey man thanks i understood it. But i have one more problem this code:
    $flintstones = "\n or modern stoneage"; if ($flintstones =~ m/(\w*) or \w* \1/){ print "True\n"; }
    is returning true even though the conditions if ($flintstones =~ m/(\w*) or \w* \1/) is false
      You are being mislead by the evil *!

      * means= match ZERO or more of the previous character or group. In this case it is the ZERO that wins. The matched part of the string is ' or modern'.

      Try print "True/ >$1<\n"; and you will see what got matched by the first \w*.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James