谁能解释一下perl的规则式的意思?

谁能解释一下perl的规则式的意思?

#!/usr/bin/perl
use strict;
use warnings;

my $string1 = "madam im adam";
my $string2 = "the motto means something";
my $string3 = "no palindrome here";

findPalindrome( $string1 );
findPalindrome( $string2 );
findPalindrome( $string3 );

sub findPalindrome
{
   my $string = shift();

   if ( $string =~
           /(\w)\W*(\w)\W*(\w)\W*(\w)\W*\4\W*\3\W*\2\W*\1/
        or $string =~
           /(\w)\W*(\w)\W*(\w)\W*(\w)\W*\3\W*\2\W*\1/ ) {
      print "$string - ",
            "has a palindrome of at least 7 characters.\n";
   }
   else {
      print "$string - has no long palindromes.\n";
   }
}

那位大侠能解释一下:/(\w)\W*(\w)\W*(\w)\W*(\w)\W*\4\W*\3\W*\2\W*\1/和/(\w)\W*(\w)\W*(\w)\W*(\w)\W*\3\W*\2\W*\1/的意思
看不懂,学习一下 !!!
建议楼主去本版的精华部分了解下perl的正则表达式吧!
你这正则表达式挺简单的!
这个正则式用来判断字符串

has a palindrome of at least 7 characters

或者

has no long palindromes

人家自己说的很清楚嘛
为什么不能讲一下呢?不要吝啬吗。


QUOTE:
原帖由 cobrawgl 于 2008-5-4 16:42 发表
这个正则式用来判断字符串

has a palindrome of at least 7 characters

或者

has no long palindromes

人家自己说的很清楚嘛

只是想有人给详细的解释一下,那2个表达式的意思。虚心向大家请教。谢谢!主要是后面的\4 \3 \2 \1不太明白。

Learning Perl 4th version

8.6. The Match Variables
So far, when we've put parentheses into patterns, they've been used only for their ability to group parts of a pattern together. But parentheses also trigger the regular expression engine's memory. The memory holds the part of the string matched by the part of the pattern inside parentheses. If there are more than one pair of parentheses, there will be more than one memory. Each regular expression memory holds part of the original string, not part of the pattern.

Since these variables hold strings, they are scalar variables; in Perl, they have names like $1 and $2. There are as many of these variables as there are pairs of memory parentheses in the pattern. As you'd expect, $4 means the string matched by the fourth set of parentheses.

[Copy to clipboard] [ - ]
CODE:
/(\w)  ----> \1 or $1
\W*
(\w)  ----> \2 or $2
\W*
(\w)  ----> \3 or $3
\W*
(\w)  ----> \4 or $4
\W*
\4
\W*
\3
\W*
\2
\W*
\1/

perl 还会提醒你,\1 最好写成 $1 之类的。