For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2005 > Regex c code









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Regex c code
Jerry Preston

2005-02-23, 8:55 am

Hi!

This seems to so simple, but I do not see what I am doing wrong.

I want to get the function name from a string of c code:

$line = "Func001( 1,2,3,4 )";

Using $line =~ /\(/;
Gives $1 = Func001;

Works great, but not on the following

$line = "Var_name[ 0 ] = Func002( d,e,r,t,)";

Using $line =~ /\(/;
Gives $1 = Var_name[ 0 ]=;

What am I missing?

Thanks,

Jerry



Jerry Preston

2005-02-23, 8:55 am

Correction!

I am using /(\w+)\(/ and not /\(/.

Thanks,

Jerry

-----Original Message-----
From: Jerry Preston [mailto:g-preston1@ti.com]
Sent: Tuesday, February 22, 2005 9:17 PM
To: 'Perl Beginners'
Subject: Regex c code


Hi!

This seems to so simple, but I do not see what I am doing wrong.

I want to get the function name from a string of c code:

$line = "Func001( 1,2,3,4 )";

Using $line =~ /\(/;
Gives $1 = Func001;

Works great, but not on the following

$line = "Var_name[ 0 ] = Func002( d,e,r,t,)";

Using $line =~ /\(/;
Gives $1 = Var_name[ 0 ]=;

What am I missing?

Thanks,

Jerry



Jerry Preston

2005-02-23, 8:55 am

Brandon,

Tried your code and I am still getting "Var_name[ 0 ] =3D" and not =
"Func002";

Thanks,

Jerry

-----Original Message-----
From: Brandon Willis [mailto:brandon.willis@silverorb.net]=20
Sent: Tuesday, February 22, 2005 9:30 PM
To: Jerry Preston
Cc: 'Perl Beginners'
Subject: RE: Regex c code


As far as I can see you aren't missing anything.=20
perl -e '$a=3D"foo [ 0 ] =3D function(1,2,3,4)"; $a=3D~/(\w+)\(/; print =
$1' works
great for me. =3D)

But you might think about using this:
perl -e '$a=3D" foo [ 0 ] =3D function (1,2,3,4)"; =
$a=3D~/\b(\w+)\b\s*?\(/; print
$1'

The zero width assertion \b and the non-greedy space collapse of \s*? =
handle
a few more simple cases.

Cheers!
--Brandon

On Tue, 2005-02-22 at 21:19, Jerry Preston wrote:
> Correction!
>=20
> I am using /(\w+)\(/ and not /\(/.
>=20
> Thanks,
>=20
> Jerry
>=20
> -----Original Message-----
> From: Jerry Preston [mailto:g-preston1@ti.com]
> Sent: Tuesday, February 22, 2005 9:17 PM
> To: 'Perl Beginners'
> Subject: Regex c code
>=20
>=20
> Hi!
>=20
> This seems to so simple, but I do not see what I am doing wrong.
>=20
> I want to get the function name from a string of c code:
>=20
> $line =3D "Func001( 1,2,3,4 )";
>=20
> Using $line =3D~ /\(/;
> Gives $1 =3D Func001;
>=20
> Works great, but not on the following
>=20
> $line =3D "Var_name[ 0 ] =3D Func002( d,e,r,t,)";
>=20
> Using $line =3D~ /\(/;
> Gives $1 =3D Var_name[ 0 ]=3D;
>=20
> What am I missing?
>=20
> Thanks,
>=20
> Jerry
>=20
> =20
>=20


Jerry Preston

2005-02-23, 8:55 am

Brandon,

I found my error! This part is working correctly.

Thanks for your help!

Jerry

-----Original Message-----
From: Jerry Preston [mailto:g-preston1@ti.com]=20
Sent: Tuesday, February 22, 2005 9:47 PM
To: 'Brandon Willis'
Cc: 'Perl Beginners'
Subject: RE: Regex c code


Brandon,

Tried your code and I am still getting "Var_name[ 0 ] =3D" and not =
"Func002";

Thanks,

Jerry

-----Original Message-----
From: Brandon Willis [mailto:brandon.willis@silverorb.net]=20
Sent: Tuesday, February 22, 2005 9:30 PM
To: Jerry Preston
Cc: 'Perl Beginners'
Subject: RE: Regex c code


As far as I can see you aren't missing anything.=20
perl -e '$a=3D"foo [ 0 ] =3D function(1,2,3,4)"; $a=3D~/(\w+)\(/; print =
$1' works
great for me. =3D)

But you might think about using this:
perl -e '$a=3D" foo [ 0 ] =3D function (1,2,3,4)"; =
$a=3D~/\b(\w+)\b\s*?\(/; print
$1'

The zero width assertion \b and the non-greedy space collapse of \s*? =
handle
a few more simple cases.

Cheers!
--Brandon

On Tue, 2005-02-22 at 21:19, Jerry Preston wrote:
> Correction!
>=20
> I am using /(\w+)\(/ and not /\(/.
>=20
> Thanks,
>=20
> Jerry
>=20
> -----Original Message-----
> From: Jerry Preston [mailto:g-preston1@ti.com]
> Sent: Tuesday, February 22, 2005 9:17 PM
> To: 'Perl Beginners'
> Subject: Regex c code
>=20
>=20
> Hi!
>=20
> This seems to so simple, but I do not see what I am doing wrong.
>=20
> I want to get the function name from a string of c code:
>=20
> $line =3D "Func001( 1,2,3,4 )";
>=20
> Using $line =3D~ /\(/;
> Gives $1 =3D Func001;
>=20
> Works great, but not on the following
>=20
> $line =3D "Var_name[ 0 ] =3D Func002( d,e,r,t,)";
>=20
> Using $line =3D~ /\(/;
> Gives $1 =3D Var_name[ 0 ]=3D;
>=20
> What am I missing?
>=20
> Thanks,
>=20
> Jerry
>=20
> =20
>=20



--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Jerry Preston

2005-02-23, 8:55 am

I am on a sun box Perl v 5.6.1.

This is what I got:

**$` =| Var_name[ 0 ]= | $& =|Func002(| $' =|d,e,r,t ); | $1 =|Func002|

I need to look at what you are doing here.

Jerry


-----Original Message-----
From: Brandon Willis [mailto:brandon.willis@silverorb.net]
Sent: Tuesday, February 22, 2005 10:04 PM
To: Jerry Preston
Cc: 'Perl Beginners'
Subject: RE: Regex c code


Very strange... What platform and perl are you on? I'm on RH7.3 perl 5.6.1.
And could you try this:

my $a=" foo [ 0 ] = function(a,b,v,t)";
$a=~/\b(\w+)\b\s*?\(/;
print "\$` =|$`|\n\$& =|$&|\n\$' =|$'|\n\$1 =|$1|\n";
__END__


My output is this:
$` =| foo [ 0 ] = |
$& =|function(|
$' =|a,b,v,t)|
$1 =|function|

What is your output?

--Brandon

P.S. I just tried this on Fedora Core 2 with perl 5.8.3 and got the same
output.

On Tue, 2005-02-22 at 21:46, Jerry Preston wrote:
> Brandon,
>
> Tried your code and I am still getting "Var_name[ 0 ] =" and not
> "Func002";
>
> Thanks,
>
> Jerry
>
> -----Original Message-----
> From: Brandon Willis [mailto:brandon.willis@silverorb.net]
> Sent: Tuesday, February 22, 2005 9:30 PM
> To: Jerry Preston
> Cc: 'Perl Beginners'
> Subject: RE: Regex c code
>
>
> As far as I can see you aren't missing anything.
> perl -e '$a="foo [ 0 ] = function(1,2,3,4)"; $a=~/(\w+)\(/; print $1'

works
> great for me. =)
>
> But you might think about using this:
> perl -e '$a=" foo [ 0 ] = function (1,2,3,4)"; $a=~/\b(\w+)\b\s*?\(/;
> print $1'
>
> The zero width assertion \b and the non-greedy space collapse of \s*?
> handle a few more simple cases.
>
> Cheers!
> --Brandon
>
> On Tue, 2005-02-22 at 21:19, Jerry Preston wrote:
>
>


Brandon Willis

2005-02-23, 8:55 am

As far as I can see you aren't missing anything.
perl -e '$a="foo [ 0 ] = function(1,2,3,4)"; $a=~/(\w+)\(/; print $1'
works great for me. =)

But you might think about using this:
perl -e '$a=" foo [ 0 ] = function (1,2,3,4)"; $a=~/\b(\w+)\b\s*?\(/;
print $1'

The zero width assertion \b and the non-greedy space collapse of \s*?
handle a few more simple cases.

Cheers!
--Brandon

On Tue, 2005-02-22 at 21:19, Jerry Preston wrote:
> Correction!
>
> I am using /(\w+)\(/ and not /\(/.
>
> Thanks,
>
> Jerry
>
> -----Original Message-----
> From: Jerry Preston [mailto:g-preston1@ti.com]
> Sent: Tuesday, February 22, 2005 9:17 PM
> To: 'Perl Beginners'
> Subject: Regex c code
>
>
> Hi!
>
> This seems to so simple, but I do not see what I am doing wrong.
>
> I want to get the function name from a string of c code:
>
> $line = "Func001( 1,2,3,4 )";
>
> Using $line =~ /\(/;
> Gives $1 = Func001;
>
> Works great, but not on the following
>
> $line = "Var_name[ 0 ] = Func002( d,e,r,t,)";
>
> Using $line =~ /\(/;
> Gives $1 = Var_name[ 0 ]=;
>
> What am I missing?
>
> Thanks,
>
> Jerry
>
>
>


Brandon Willis

2005-02-23, 8:55 am

Very strange... What platform and perl are you on? I'm on RH7.3 perl
5.6.1. And could you try this:

my $a=" foo [ 0 ] = function(a,b,v,t)";
$a=~/\b(\w+)\b\s*?\(/;
print "\$` =|$`|\n\$& =|$&|\n\$' =|$'|\n\$1 =|$1|\n";
__END__


My output is this:
$` =| foo [ 0 ] = |
$& =|function(|
$' =|a,b,v,t)|
$1 =|function|

What is your output?

--Brandon

P.S. I just tried this on Fedora Core 2 with perl 5.8.3 and got the same
output.

On Tue, 2005-02-22 at 21:46, Jerry Preston wrote:
> Brandon,
>
> Tried your code and I am still getting "Var_name[ 0 ] =" and not "Func002";
>
> Thanks,
>
> Jerry
>
> -----Original Message-----
> From: Brandon Willis [mailto:brandon.willis@silverorb.net]
> Sent: Tuesday, February 22, 2005 9:30 PM
> To: Jerry Preston
> Cc: 'Perl Beginners'
> Subject: RE: Regex c code
>
>
> As far as I can see you aren't missing anything.
> perl -e '$a="foo [ 0 ] = function(1,2,3,4)"; $a=~/(\w+)\(/; print $1' works
> great for me. =)
>
> But you might think about using this:
> perl -e '$a=" foo [ 0 ] = function (1,2,3,4)"; $a=~/\b(\w+)\b\s*?\(/; print
> $1'
>
> The zero width assertion \b and the non-greedy space collapse of \s*? handle
> a few more simple cases.
>
> Cheers!
> --Brandon
>
> On Tue, 2005-02-22 at 21:19, Jerry Preston wrote:
>
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com