For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2007 > Regarding pattern matching









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 Regarding pattern matching
Dharshana Eswaran

2007-05-17, 7:58 am

Hi All,

I am trying to extract few strings from a text file. The pattern of the text
stored in the file is as follows:

#define MNSS_FACILITY_IND_ID (TF_MNSS_MESSAGE_CATEGORY + 0x01) /* @LOG
MNSS_MESSAGE_T */


I need to extract MNSS_FACILITY_IND_ID, TF_MNSS_MESSAGE_CATEGORY + 0x01 and
MNSS_MESSAGE_T.

I tried

next unless /#define\s+(\w+)\s+\(([^)]+)\s+\/\*\s+@LOG\s+(\w+)\*\//;

my $name = $1;
my ($base, $offset) = $2 =~ /\w+/g;
my $Struct = $3;

I am unable to find out the error.

Can anyone help m in this?

Thanks and Regards,
Dharshana

Rob Coops

2007-05-17, 7:58 am

How about this?


#!/usr/local/bin/perl

use strict;
use warnings;

my $string = '#define MNSS_FACILITY_IND_ID (TF_MNSS_MESSAGE_CATEGORY +
0x01) /* @LOG MNSS_MESSAGE_T */';

my @parts = $string =~ m/\S+?(\w+)\s+?\((\w+.*?)\).*?\@\w{3}\s(\w+).*/;

use Data::Dumper;

print Dumper @parts;



OUTPUT
-------------------------------------------------
$VAR1 = 'NSS_FACILITY_IND_ID';
$VAR2 = 'TF_MNSS_MESSAGE_CATEGORY + 0x01';
$VAR3 = 'MNSS_MESSAGE_T';
-------------------------------------------------



On 5/17/07, Dharshana Eswaran <dharshana.ve@gmail.com> wrote:
>
> Hi All,
>
> I am trying to extract few strings from a text file. The pattern of the
> text
> stored in the file is as follows:
>
> #define MNSS_FACILITY_IND_ID (TF_MNSS_MESSAGE_CATEGORY + 0x01) /*
> @LOG
> MNSS_MESSAGE_T */
>
>
> I need to extract MNSS_FACILITY_IND_ID, TF_MNSS_MESSAGE_CATEGORY + 0x01
> and
> MNSS_MESSAGE_T.
>
> I tried
>
> next unless /#define\s+(\w+)\s+\(([^)]+)\s+\/\*\s+@LOG\s+(\w+)\*\//;
>
> my $name = $1;
> my ($base, $offset) = $2 =~ /\w+/g;
> my $Struct = $3;
>
> I am unable to find out the error.
>
> Can anyone help m in this?
>
> Thanks and Regards,
> Dharshana
>


Rob Coops

2007-05-17, 7:58 am

On 5/17/07, Rob Coops <rcoops@gmail.com> wrote:
>
> How about this?
>
>
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> my $string = '#define MNSS_FACILITY_IND_ID (TF_MNSS_MESSAGE_CATEGORY
> + 0x01) /* @LOG MNSS_MESSAGE_T */';
>
> my @parts = $string =~ m/\s+?(\w+)\s+?\((\w+.*?)\).*?\@\w{3}\s(\w+).*/;
>
> use Data::Dumper;
>
> print Dumper @parts;
>
> ----- Had to change that one \S to \s (oops) -----
>
> OUTPUT
> -------------------------------------------------
> $VAR1 = 'NSS_FACILITY_IND_ID';
> $VAR2 = 'TF_MNSS_MESSAGE_CATEGORY + 0x01';
> $VAR3 = 'MNSS_MESSAGE_T';
> -------------------------------------------------
>
>
>
> On 5/17/07, Dharshana Eswaran <dharshana.ve@gmail.com> wrote:
>
>


Dharshana Eswaran

2007-05-17, 7:58 am

Thank you.... But i want to try without using any perl modules. Can you
suggest some way with no modules used in that?

Thanks and Regards,
Dharshana

On 5/17/07, Rob Coops <rcoops@gmail.com> wrote:
>
>
>
> On 5/17/07, Rob Coops <rcoops@gmail.com> wrote:
>


John Moon

2007-05-17, 7:58 am

Subject: Regarding pattern matching

Hi All,

I am trying to extract few strings from a text file. The pattern of the
text
stored in the file is as follows:

#define MNSS_FACILITY_IND_ID (TF_MNSS_MESSAGE_CATEGORY + 0x01) /*
@LOG
MNSS_MESSAGE_T */


I need to extract MNSS_FACILITY_IND_ID, TF_MNSS_MESSAGE_CATEGORY + 0x01
and
MNSS_MESSAGE_T.

I tried

next unless /#define\s+(\w+)\s+\(([^)]+)\s+\/\*\s+@LOG\s+(\w+)\*\//;

my $name =3D $1;
my ($base, $offset) =3D $2 =3D~ /\w+/g;
my $Struct =3D $3;

I am unable to find out the error.

Can anyone help m in this?

Thanks and Regards,
Dharshana

[>>] One way=20
my $data =3D q{#define MNSS_FACILITY_IND_ID
(TF_MNSS_MESSAGE_CATEGORY + 0x01) /* @LOG MNSS_MESSAGE_T */};
my @list =3D $data =3D~ /^#define\s*(.+)\s*\((.+)\)\s*\/\*\s*(.+)...$/;
print "$_\n" for (@list);

hope this helps
jwm
Rob Coops

2007-05-17, 7:58 am

Sure, though I do not see why you would not want to use strict and warnings
(you should should should or the people on this list will hunt you down and
.....) anyway Data::Dumper was just there for convenience:


#!/usr/local/bin/perl

#use strict;
#use warnings;

my $string = '#define MNSS_FACILITY_IND_ID (TF_MNSS_MESSAGE_CATEGORY +
0x01) /* @LOG MNSS_MESSAGE_T */';

my @parts = $string =~ m/\s+?(\w+)\s+?\((\w+.*?)\).*?\@\w{3}\s(\w+).*/;

#use Data::Dumper;

#print Dumper @parts;

print $1 . "\n" . $2 . "\n" . $3 . "\n";
print "#----- or -----#\n";
foreach ( @parts ) { print $_ . "\n"; }



On 5/17/07, Dharshana Eswaran <dharshana.ve@gmail.com> wrote:
>
> Thank you.... But i want to try without using any perl modules. Can you
> suggest some way with no modules used in that?
>
> Thanks and Regards,
> Dharshana
>
> On 5/17/07, Rob Coops <rcoops@gmail.com> wrote:
>


Matthew J. Avitable

2007-05-17, 7:58 am

Dharshana Eswaran wrote:
> Hi All,
>
> I am trying to extract few strings from a text file. The pattern of
> the text
> stored in the file is as follows:
>
> #define MNSS_FACILITY_IND_ID (TF_MNSS_MESSAGE_CATEGORY + 0x01)
> /* @LOG
> MNSS_MESSAGE_T */
>
>
> I need to extract MNSS_FACILITY_IND_ID, TF_MNSS_MESSAGE_CATEGORY +
> 0x01 and
> MNSS_MESSAGE_T.
>
> I tried
>
> next unless /#define\s+(\w+)\s+\(([^)]+)\s+\/\*\s+@LOG\s+(\w+)\*\//;
>
> my $name = $1;
> my ($base, $offset) = $2 =~ /\w+/g;
> my $Struct = $3;
>


Darshana,

You were missing a literal closing paren after the offset and some
spaces right at the end. Here's the fix:

next unless /#define\s+(\w+)\s+\(([^)]+)\)\s+\/\*\s+\@LOG\s+(\w+)\s+\*\//;

And here is a possible alternative. I'm not sure what your other lines
to match look like, so your mileage may vary.

my ($name, $base, $offset, $struct) =
$line =~ m!#define\s+(\w+)\s+\((\w+).*?(\w+)\).*?\s+(\w+)!;

Hope this helps,
-m



Dharshana Eswaran

2007-05-17, 9:59 pm

Of Course Rob, i always use strict and warnings... Since i had to write a
pseudocode, i did not write them. But i wil lsurely keep this in mind. :-)
Thank you... :-)

On 5/17/07, Rob Coops <rcoops@gmail.com> wrote:
>
> Sure, though I do not see why you would not want to use strict and
> warnings (you should should should or the people on this list will hunt you
> down and ....) anyway Data::Dumper was just there for convenience:
>
>
> #!/usr/local/bin/perl
>
> #use strict;
> #use warnings;
>
> my $string = '#define MNSS_FACILITY_IND_ID (TF_MNSS_MESSAGE_CATEGORY
> + 0x01) /* @LOG MNSS_MESSAGE_T */';
>
> my @parts = $string =~ m/\s+?(\w+)\s+?\((\w+.*?)\).*?\@\w{3}\s(\w+).*/;
>
> #use Data::Dumper;
>
> #print Dumper @parts;
>
> print $1 . "\n" . $2 . "\n" . $3 . "\n";
> print "#----- or -----#\n";
> foreach ( @parts ) { print $_ . "\n"; }
>
>
>
> On 5/17/07, Dharshana Eswaran <dharshana.ve@gmail.com> wrote:
>


Abamaeme9

2007-05-18, 5:06 am

Cute blonde with REALLY big titties!
http://trully-bigtits.info/play.asp?bigtits218571
Sponsored Links







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

Copyright 2009 codecomments.com