3

在 SO 和 Google 上进行了大量搜索后,我求助于发布一个新问题。我正在使用 TextWrangler 尝试编写一个正则表达式,这将为我提供多行模式的最短匹配项。

基本上,

ہے\tVM

是我正在寻找的字符串(一个由制表符分隔的阿拉伯语单词与其词性标签)。困难的是我想搜索包含该字符串的所有单个句子。这是我到目前为止所拥有的:

/(<Sentence id='\d+'>(?:[^<]|<(?!\/Sentence>))*ہے\tVM(?:[^<]|<(?!\/Sentence>))*<\/Sentence>)/

我正在查看的文件是用 CML 编码的,所以我的部分问题是你们中是否有人知道 MAC 的 CML 解析器?

另一个明显的选择是编写一个 Perl 脚本——在这里,我再次感谢任何指向一个简单解决方案的建议。


我当前的脚本是:

use open ':encoding(utf8)';
use Encode;
binmode(STDOUT, ":utf8");
binmode(STDIN, ":utf8");

my $word = Encode::decode_utf8("ہے");

my @files = glob("*.posn");

foreach my $file (@files) {
    open FILE, "<$file" or die "Error opening file $file ($!)";
    my $file = do {local $/; <FILE>};
    close FILE or die $!;
    if ($file =~ /(<Sentence id='\d+'>(?:[^<]|<(?!\/Sentence>))*$word\tVM(?:[^<]|<(?!\/Sentence>))*<\/Sentence>)/g) {
            print STDOUT "$1\n\n\n\n";
            push(@matches, "$1\n\n");
            }
}

open(OUTPUT, ">matches.txt");
print OUTPUT "@matches";
close(OUTPUT);
4

1 回答 1

0

您可能会在输入中出现更多字符串,因此请搜索所有字符串...

我相信你的代码应该是这样的>>

use open ':encoding(utf8)';
use Encode;

binmode(STDOUT, ":utf8");
binmode(STDIN,  ":utf8");

my $word = Encode::decode_utf8("ہے");
my @files = glob("*.posn");
my @matches = ();

foreach my $file (@files) {
  open FILE, "<$file" or die "Error opening file $file ($!)";
  my $file = do {local $/; <FILE>};
  close FILE or die $!;
  my @occurrences = $file =~ /<Sentence id='\d+'>(?:[^<]|<(?!\/Sentence>))*$word\tVM(?:[^<]|<(?!\/Sentence>))*<\/Sentence>/g;
  print STDOUT "$_\n\n\n\n" for (@occurrences);
  push (@matches, "$_\n\n") for (@occurrences);
}

open (OUTPUT, ">matches.txt");
print OUTPUT  "@matches";
close(OUTPUT);

在此处了解有关正则表达式的更多信息。

于 2012-11-15T12:08:33.637 回答