匹配第一次出现的角色

时间:2014-01-24 11:45:21

标签: regex perl pattern-matching

我一直在阅读这篇文章,但它还没有解决我的问题: Regex: matching up to the first occurrence of a character

我有这个变量:

$attributes = ID=CUFF.1;Name=CG12402-RB;Note=Parial_gene

我写过这个剧本:

if ($attributes  =~ /Name=([^;]*)/)  { 
            $genename =  $-[0];
         $name = substr($attributes, $genename); 

如果我打印$name,这就是输出:Name=CG12402;Note=Parial_gene

但我想要我的输出:Name=CG12402

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

尝试使用:

/(Name=[^;]+)-/

您的原始正则表达式/Name=([^;]*)/将捕获文字Name=之后的任何字符,直到;

但是,对于您提供的正则表达式的示例,不应该产生您所说的结果。它应该捕获:CG12402-RB

相关问题