Bala
Bala

Reputation: 390

parsing of string using regex in perl

DESCR: "10GE SR" 

i need match this above part which is part of my rest of the string. Im using regex in perl. i tried

if ($line =~ /DESCR: \"([a-zA-Z0-9)\"/) { 
   print "$1\n";
}

but im not able to understand how to consider spaces inside my string. these spaces can occur any where within the quotes. can someone help me out.

Upvotes: 0

Views: 498

Answers (3)

gangabass
gangabass

Reputation: 10666

It may be simpler:

if ( $line =~ /DESCR: "([^"]+)"/ ) { 
   print "$1\n";
}

Upvotes: 0

godspeedlee
godspeedlee

Reputation: 672

Take a look, this pattern can match double quoted in string:

if ($line =~ /DESCR: \"((?:[^\\"]|\\.)*)\"/) { 
   print "$1\n";
}

Upvotes: 0

BSen
BSen

Reputation: 187

$str = 'DESCR: "10GE SR"';

if ($str =~ /DESCR: \"([a-zA-Z0-9\s]+)\"/) { 
    print "$1\n";
}

Upvotes: 2

Related Questions