Regex 多行正则表达式搜索
在SO和Google上进行了大量搜索之后,我提出了一个新问题。我正在与TextWrangler合作,试图编写一个正则表达式,它将为我提供多行模式的最短匹配 基本上Regex 多行正则表达式搜索,regex,perl,grep,textwrangler,Regex,Perl,Grep,Textwrangler,在SO和Google上进行了大量搜索之后,我提出了一个新问题。我正在与TextWrangler合作,试图编写一个正则表达式,它将为我提供多行模式的最短匹配 基本上 ہے\tVM 是我要查找的字符串(由制表符与其词性标记分隔的阿拉伯语单词)。困难的是,我想搜索包含该字符串的所有单个句子。以下是我到目前为止的情况: /(<Sentence id='\d+'>(?:[^<]|<(?!\/Sentence>))*ہے\tVM(?:[^<]|<(?!\/Sent
ہے\tVM
是我要查找的字符串(由制表符与其词性标记分隔的阿拉伯语单词)。困难的是,我想搜索包含该字符串的所有单个句子。以下是我到目前为止的情况:
/(<Sentence id='\d+'>(?:[^<]|<(?!\/Sentence>))*ہے\tVM(?:[^<]|<(?!\/Sentence>))*<\/Sentence>)/
/((?:[^输入中可能会出现更多字符串,因此请搜索所有字符串
我相信你的代码应该是这样的>>
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);
使用open':编码(utf8)';
使用编码;
binmode(标准输出,“:utf8”);
binmode(标准输入“:utf8”);
我的$word=Encode::decode_utf8(“ہے”);
my@files=glob(“*.posn”);
我的@matches=();
foreach my$文件(@files){
打开文件“matches.txt”);
打印输出“@matches”;
关闭(输出);
了解有关正则表达式的更多信息。您可能想在时使用,而不是如果;您当前的代码在每个文件中只能报告一个匹配项。很棒!是的,一个文件中可能会出现多次--我认识到了我的严重错误。感谢我的@occurrencess=$file=~…
,我很高兴我不知道这个结构。
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);