Chinaunix

标题: 谁能解释一下perl的规则式的意思? [打印本页]

作者: paub    时间: 2008-05-04 15:51
标题: 谁能解释一下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/的意思
作者: upperc    时间: 2008-05-04 16:03
看不懂,学习一下 !!!
作者: changejun    时间: 2008-05-04 16:07
建议楼主去本版的精华部分了解下perl的正则表达式吧!
你这正则表达式挺简单的!
作者: cobrawgl    时间: 2008-05-04 16:42
这个正则式用来判断字符串

has a palindrome of at least 7 characters

或者

has no long palindromes

人家自己说的很清楚嘛
作者: paub    时间: 2008-05-04 16:42
标题: 回复 #3 changejun 的帖子
为什么不能讲一下呢?不要吝啬吗。
作者: paub    时间: 2008-05-04 16:45
原帖由 cobrawgl 于 2008-5-4 16:42 发表
这个正则式用来判断字符串

has a palindrome of at least 7 characters

或者

has no long palindromes

人家自己说的很清楚嘛



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

[ 本帖最后由 paub 于 2008-5-4 16:47 编辑 ]
作者: cobrawgl    时间: 2008-05-04 16:53
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.


  1. /(\w)  ----> \1 or $1
  2. \W*
  3. (\w)  ----> \2 or $2
  4. \W*
  5. (\w)  ----> \3 or $3
  6. \W*
  7. (\w)  ----> \4 or $4
  8. \W*
  9. \4
  10. \W*
  11. \3
  12. \W*
  13. \2
  14. \W*
  15. \1/
复制代码

作者: cobrawgl    时间: 2008-05-04 16:58
perl 还会提醒你,\1 最好写成 $1 之类的。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2