Home: Perl Programming Help: Regular Expressions:
Problem extracting an expression



jkepler
Novice

Jul 22, 2014, 2:01 AM


Views: 21837
Problem extracting an expression

Hi,

I'm trying to extract an expression from a line. Here's what I'm doing:

my $exp = $line =~ m/[FAR PASCAL|FAR PASCAL_CONV]\ (.*)\(/gi;

with $1 I get the expression I want, but also the sring PASCAL !
The $line exp is of the type:

FAR PASCAL what_i_want(etc...)
or
FAR PASCAL_CONV what_i_want(etc...)

What am I doing wrong...? Frown

Kind regards,

jkepler


BillKSmith
Veteran

Jul 22, 2014, 7:40 AM


Views: 21830
Re: [jkepler] Problem extracting an expression

Your biggest mistake is to use '[..]' when you mean non-capturing parenthesis '(?:...)'. The /g flag is not required for your example. $exp does not contain what you expect because if forces scalar context.


Code
use strict; 
use warnings;
#my $line = 'FAR PASCAL_CONV what_i_want(etc...)';
my $line = 'FAR PASCAL what_i_want(etc...)';
my ($exp) =
$line =~ m/
FAR\sPASCAl # Required
(?:_CONV)? # Optional - Do not capture
\s # Required
(.*) # Capture Desired Output
\( # Required
/xi;

print $exp, "\n";

Good Luck,
Bill


hwnd
User

Jul 22, 2014, 8:13 AM


Views: 21828
Re: [jkepler] Problem extracting an expression


Code
my ($exp) = $line =~ m/PASCAL\S*\s*(.*)\(/i;



jkepler
Novice

Jul 22, 2014, 12:12 PM


Views: 21820
Re: [recruiter] Problem extracting an expression

Thank you very much :)

JKepler