regexperlpattern-matching

Matching up to the first occurrence of a character regex gets unexpected output


I have been reading this post, but it has not solved my problem: Regex: matching up to the first occurrence of a character

I have this variable:

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

I have written this script:

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

If I print $name this is the output: Name=CG12402;Note=Parial_gene

But I want my output like this: Name=CG12402


Solution

  • Try using this:

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

    Your original regex /Name=([^;]*)/ will capture any character after a literal Name= up to ;

    However, for the example you provide your regex shouldn't produce the result you said it does. It should capture: CG12402-RB