使用Perl正则又一例

向后引用,不知道有多少人使用过,以前写过一篇介绍Perl正则的文章,这里又来显摆一下......

如果要找一个词在一个句子中有规律的出现,如果出现就打印出来,如果用C语言写,估计代码也会有个几千行,如果考虑分词技术,则是一项特别复杂的工程,Perl的向后引用则可以方便的实现其所要功能.
下面是一个例子,$str可以使用汉字来匹配,当然,.*是一种极耗资源的模糊匹配,如果只需要指定特殊的字符集就可以了.

#!/usr/bin/perl

use strict;
use warnings;

my $str = "11111.0.11111";

if ($str =~ /(.+)(.*)\1/)
{
my $word = $1;
my $spec = $2;
print "#" . $word . "#" . $spec . "#\n";
if (length($spec) < 5 && length($word) > 2)
{
print "#" . $word . "#" . $spec . "#\n";
}
}

Monthly Archives

Pages

Powered by Movable Type 7.7.2

About this Entry

This page contains a single entry by Cnangel published on May 27, 2007 8:21 PM.

IE vs FF CSS 兼容要点 was the previous entry in this blog.

规范化程序编写 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.