|
楼主 |
发表于 2004-6-10 02:43:29
|
显示全部楼层
最初由 roby 发表
当{}全部成对的情况已经解决
abc{123{}->123{
abc{123}}->123}
这种情况也有了点头绪
先睡觉
明天起来后再继续
强的。我现在只解决了成对的情况。这是代码,实现得很不好
- #!/usr/bin/perl -w
- open IN, $ARGV[0] or die("Can't open file: $!\n");
- my @array=<IN>;
- foreach my $content(@array){
- chomp $content;
- my $matched = undef;
- my @result=();
- for(my $i= length($content); $i>=0; $i--){
- my $pre = "[^{}]*{[^{}]*" x $i;
- my $suf = "[^{}]*}[^{}]*" x $i;
- if($content=~m#abc{(([^{}]*($pre {[^{}]*}$suf )*[^{}]*)*)}#x){
- push @result, $1;
- $matched = 1;
- }
- }
- print "Matched: $content -> ", &lengest(\@result), "\n" if $matched;
- print "Unmatched: $content\n" unless $matched;
- }
- close(IN);
- sub lengest
- {
- my $array = $_[0];
- my $len = 0;
- my $ret = undef;
- foreach(@$array){
- if($len < length($_)){
- $ret = $_;
- $len = length($_);
- }
- }
- return $ret;
- }
-
复制代码
所使用的测试文件:
- abc{}
- abc{abc}
- abc{abc{}}
- abc{abc{{abc}}}
- abc{abc{b}a{b}d}
- abc{abc{d}a{b}d}{a}
- abc{abc{d}a{b}d}db{a}
复制代码
输出结果:
- Use of uninitialized value in print at ./gnum.pl line 16, <IN> line 7.
- Matched: abc{} ->
- Matched: abc{abc} -> abc
- Matched: abc{abc{}} -> abc{}
- Matched: abc{abc{{abc}}} -> abc{{abc}}
- Matched: abc{abc{b}a{b}d} -> abc{b}a{b}d
- Matched: abc{abc{d}a{b}d}{a} -> abc{d}a{b}d
- Matched: abc{abc{d}a{b}d}db{a} -> abc{d}a{b}d
复制代码
那个警告,我一直没看出来是哪里出问题。 |
|