【发布时间】:2013-08-04 01:49:49
【问题描述】:
我在单个空格字符处拆分句子,然后将这些术语与哈希键进行匹配。只有当术语 100% 相似时,我才会得到匹配,并且我正在努力寻找一个完美的正则表达式,它可以匹配多个相同单词的出现。例如。让我们考虑一下我有一个术语“拮抗剂”,现在它与术语“拮抗剂”完全匹配,但无法与拮抗剂、拮抗剂或前拮抗剂、水拮抗剂等匹配。我还需要一个正则表达式来匹配诸如 MCF 之类的词的出现-7 与 MCF7 或 MC-F7 静音特殊字符等效果。
这是我到现在为止的代码; thr 评论部分是我苦苦挣扎的地方。
(注意:散列中的术语是词根形式)。
use warnings;
use strict;
use Drug;
use Stop;
open IN, "sample.txt" or die "cannot find sample";
open OUT, ">sample1.txt" or die "cannot find sample";
while (<IN>) {
chomp $_;
my $flag = 0;
my $line = lc $_;
my @full = ();
if ( $line =~ /<Sentence.*>(.*)<\/Sentence>/i ) {
my $string = $1;
chomp $string;
$string =~ s/,/ , /g;
$string =~ s/\./ \. /g;
$string =~ s/;/ ; /g;
$string =~ s/\(/ ( /g;
$string =~ s/\)/ )/g;
$string =~ s/\:/ : /g;
$string =~ s/\::/ :: )/g;
my @array = split / /, $string;
foreach my $word (@array) {
chomp $word;
if ( $word =~ /\,|\;|\.|\(|\)/g ) {
push( @full, $word );
}
if ( $Stop_words{$word} ) {
push( @full, $word );
}
if ( $Values{$word} ) {
my $term = "<Drug>$word<\/Drug>";
push( @full, $term );
}
else {
push( @full, $word );
}
# if($word=~/.*\Q$Values{$word}\E/i)#Changed this
# {
# $term="<Drug>$word</$Drug>";
# print $term,"\n";
# push(@full,$term);
# }
}
}
my $mod_str = join( " ", @full );
print OUT $mod_str, "\n";
}
【问题讨论】:
-
你应该尽量让你的问题更简洁。
-
请提供您的
sample.txt。通常,对于您的“特殊字符”情况,最简单的方法是在开始之前将它们从输入中删除。 -
sample.txt 文件是科学文本文件,我不能去掉这些字符,因为这会改变一些术语的含义
-
@TLP : 让我知道你不清楚哪一部分
-
我不认为正则表达式可以做你想做的事。对我来说听起来像是XY problem。考虑String::Compare。