带有散列键和值的正则表达式

时间:2012-10-24 20:49:38

标签: perl

我正在尝试检查哈希键中是否出现某些术语,如果我们用哈希值标记它们。我的代码标识了完全匹配,但对于稍有不同的术语则失败。此外,对于完全匹配,代码将匹配的术语标记为所有值而不是相关的值。

%Drugs = ('tamoxifen' => tablet,
        'docotaxel' => capsule,
        '5-hydroxycin' => IDK,);

$sentence="tamoxifen 4-hydroxytamoxifen tamoxifen-incoded pre-docotaxel Drugs";
@sentence=split/ /,$sentence;
@array=();
foreach $word(@sentence)
{
    chomp $word;
    for $keys(keys %Drugs)
    {
        if($Drugs{$word})
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
        }
        elsif($word=~/.*$Drugs{$keys}$/i)
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
        }
    }
    foreach $Bio(@array)
    {
        print "$Bio\n";
    }

我想要输出句子:

<tablet>tamoxifen</tablet> <tablet>4-hydroxytamoxifen</tablet> <tablet>tamoxifen-incoded<tablet> <capsule>pre-docotaxel<capsule> Drugs.(Here Drugs didn't match and hence it is left untagged)  

1 个答案:

答案 0 :(得分:2)

您正在检查完全匹配,但根据您的预期,您必须使用$ key对您的$ word进行正则表达式

试试这个 - &gt;

%Drugs = ('tamoxifen' => tablet,
        'docotaxel' => capsule,
        '5-hydroxycin' => IDK,);

$sentence="tamoxifen 4-hydroxytamoxifen tamoxifen-incoded pre-docotaxel Drugs";
@sentence=split/ /,$sentence;
@array=();
foreach $word(@sentence)
{
    chomp $word;
    for $keys(keys %Drugs)
    {
        if($word=~/.*$keys$/i)#Changed this
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
        }
        elsif($word=~/.*$Drugs{$keys}$/i)
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
        }
    }
}
foreach $Bio(@array)
  {
     print "$Bio\n";
  }

<强>更新


基于新要求

%Drugs = ('tamoxifen' => tablet,
        'docotaxel' => capsule,
        '5-hydroxycin' => IDK,);

$sentence="tamoxifen 4-hydroxytamoxifen tamoxifen-incoded pre-docotaxel Drugs";
@sentence=split/ /,$sentence;
@array=();
foreach $word(@sentence)
{
    chomp $word;
    my $flag= 0; #using a flag, I am sure there are PLENTY of ways better than this :)
    for $keys(keys %Drugs)
    {

        if($word=~/.*$keys$/i)#Changed this
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
            $flag++;
        }
        elsif($word=~/.*$Drugs{$keys}$/i)
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
            $flag++;
        }

    }
    push (@array,$word) if $flag==0;
}
foreach $Bio(@array)
  {
     print "$Bio\n";
  }