亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2310 | 回復(fù): 7
打印 上一主題 下一主題

誰能解釋一下perl的規(guī)則式的意思? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2008-05-04 15:51 |只看該作者 |倒序瀏覽
#!/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/的意思

論壇徽章:
0
2 [報告]
發(fā)表于 2008-05-04 16:03 |只看該作者
看不懂,學(xué)習(xí)一下 。!

論壇徽章:
0
3 [報告]
發(fā)表于 2008-05-04 16:07 |只看該作者
建議樓主去本版的精華部分了解下perl的正則表達式吧!
你這正則表達式挺簡單的!

論壇徽章:
0
4 [報告]
發(fā)表于 2008-05-04 16:42 |只看該作者
這個正則式用來判斷字符串

has a palindrome of at least 7 characters

或者

has no long palindromes

人家自己說的很清楚嘛

論壇徽章:
0
5 [報告]
發(fā)表于 2008-05-04 16:42 |只看該作者

回復(fù) #3 changejun 的帖子

為什么不能講一下呢?不要吝嗇嗎。

論壇徽章:
0
6 [報告]
發(fā)表于 2008-05-04 16:45 |只看該作者
原帖由 cobrawgl 于 2008-5-4 16:42 發(fā)表
這個正則式用來判斷字符串

has a palindrome of at least 7 characters

或者

has no long palindromes

人家自己說的很清楚嘛



只是想有人給詳細的解釋一下,那2個表達式的意思。虛心向大家請教。謝謝!主要是后面的\4 \3 \2 \1不太明白。

[ 本帖最后由 paub 于 2008-5-4 16:47 編輯 ]

論壇徽章:
0
7 [報告]
發(fā)表于 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/
復(fù)制代碼

論壇徽章:
0
8 [報告]
發(fā)表于 2008-05-04 16:58 |只看該作者
perl 還會提醒你,\1 最好寫成 $1 之類的。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP