Regular expression question

C

ChocMonk

Hello,
I have an array with contents as below (from a VB project file):
my @objs= ("Object={831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0;
mscomctl.ocx\n",
"Object={31B58EFA-5E2E-4651-9454-2227ABB82B62}#6.7#0;
GridControls.ocx\n",
"Object={B9AA5EAF-A9B4-11D5-96CA-0010A4B15184}#1.0#0;
CompanyLogo.ocx\n",
"Object={BDC217C8-ED16-11CD-956C-0000C04E4C0A}#1.1#0; tabctl32.ocx\n",
"Object={CA8A9783-280D-11CF-A24D-444553540000}#1.3#0; pdf.ocx\n",
"Object={64EE12AF-3495-45BC-AC02-43C29DF43376}#9.1#0;
StandardControls.ocx\n",
"Object={86CF1D34-0C5F-11D2-A9FC-0000F8754DA1}#2.0#0; mscomct2.ocx\n");

I populated the array from parsing a file with the following regexp:
my @objs = grep(/^Object=\{\w|\-*\}#\d+\.\d+#\d?; .+/,@str);

Now I'm cycling through the array trying to split it up such that for
the first string in the example above I get the following values:
my $crap = "Object="
my $guid ="{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}"
my $ver = "2.0"
my $rev = "0"
my $name = "mscomctl.ocx\n"

I tried the following which doesn't work:
for (@objs) {
chomp;
(my $crap, my $guid, my $ver, my $rev, my $name) =
/(^Object=)(\{\w|\-*\})#(\d+\.\d+)#(\d?); (.+)/;
}

WHat am I doing wrong? I thought clustering the subexpressions will
capture what I want..

Thanks!
Choc
 
T

Tore Aursand

ChocMonk said:
I have an array with contents as below (from a VB project file):
my @objs= ("Object={831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0;
mscomctl.ocx\n",
"Object={31B58EFA-5E2E-4651-9454-2227ABB82B62}#6.7#0;
GridControls.ocx\n",
"Object={B9AA5EAF-A9B4-11D5-96CA-0010A4B15184}#1.0#0;
CompanyLogo.ocx\n",
"Object={BDC217C8-ED16-11CD-956C-0000C04E4C0A}#1.1#0; tabctl32.ocx\n",
"Object={CA8A9783-280D-11CF-A24D-444553540000}#1.3#0; pdf.ocx\n",
"Object={64EE12AF-3495-45BC-AC02-43C29DF43376}#9.1#0;
StandardControls.ocx\n",
"Object={86CF1D34-0C5F-11D2-A9FC-0000F8754DA1}#2.0#0; mscomct2.ocx\n");

I populated the array from parsing a file with the following regexp:
my @objs = grep(/^Object=\{\w|\-*\}#\d+\.\d+#\d?; .+/,@str);

Now I'm cycling through the array trying to split it up such that for
the first string in the example above I get the following values:
my $crap = "Object="
my $guid ="{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}"
my $ver = "2.0"
my $rev = "0"
my $name = "mscomctl.ocx\n"

I tried the following which doesn't work:
for (@objs) {
chomp;
(my $crap, my $guid, my $ver, my $rev, my $name) =
/(^Object=)(\{\w|\-*\})#(\d+\.\d+)#(\d?); (.+)/;
}

WHat am I doing wrong? I thought clustering the subexpressions will
capture what I want..

No need to make it harder than it really is, I guess;

while ( <DATA> ) {
chomp;
if ( m,^(Object=)({.+})#(.+)#(.+); (.+), ) {
print "$1\n$2\n$3\n$4\n$5\n\n";
}
}

__DATA__
Object={831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0; mscomctl.ocx
Object={31B58EFA-5E2E-4651-9454-2227ABB82B62}#6.7#0; GridControls.ocx
Object={B9AA5EAF-A9B4-11D5-96CA-0010A4B15184}#1.0#0; CompanyLogo.ocx
Object={BDC217C8-ED16-11CD-956C-0000C04E4C0A}#1.1#0; tabctl32.ocx
Object={CA8A9783-280D-11CF-A24D-444553540000}#1.3#0; pdf.ocx
Object={64EE12AF-3495-45BC-AC02-43C29DF43376}#9.1#0; StandardControls.ocx
Object={86CF1D34-0C5F-11D2-A9FC-0000F8754DA1}#2.0#0; mscomct2.ocx


--
Tore Aursand <[email protected]>
"Writing is a lot like sex. At first you do it because you like it.
Then you find yourself doing it for a few close friends and people you
like. But if you're any good at all, you end up doing it for money."
(Unknown)
 
G

Gunnar Hjalmarsson

ChocMonk said:
/(^Object=)(\{\w|\-*\})#(\d+\.\d+)#(\d?); (.+)/;

You need grouping to limit the effect of the alternation character.

/(^Object=)(\{(?:\w|\-)*\})#(\d+\.\d+)#(\d?); (.+)/;
------------------^^^-----^
 
S

Scott Bryce

ChocMonk said:
I have an array with contents as below

Now I'm cycling through the array trying to split it up such that for
the first string in the example above I get the following values:
my $crap = "Object="
my $guid ="{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}"
my $ver = "2.0"
my $rev = "0"
my $name = "mscomctl.ocx\n"

I tried the following which doesn't work:
for (@objs) {
chomp;
(my $crap, my $guid, my $ver, my $rev, my $name) =
/(^Object=)(\{\w|\-*\})#(\d+\.\d+)#(\d?); (.+)/;
}

Regex matching returns true or false. It does not return a list of items
matched.

You should try splitting the data. The code below assumes that $crap is
really crap, that is, you don't intend to keep it. If you intend to keep
it, you could easily append the '=' back on to it.

use strict;
use warnings;

my @objs=
("Object={831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0;mscomctl.ocx\n",
"Object={31B58EFA-5E2E-4651-9454-2227ABB82B62}#6.7#0;GridControls.ocx\n",
"Object={B9AA5EAF-A9B4-11D5-96CA-0010A4B15184}#1.0#0;CompanyLogo.ocx\n",
"Object={BDC217C8-ED16-11CD-956C-0000C04E4C0A}#1.1#0; tabctl32.ocx\n",
"Object={CA8A9783-280D-11CF-A24D-444553540000}#1.3#0; pdf.ocx\n",
"Object={64EE12AF-3495-45BC-AC02-43C29DF43376}#9.1#0;StandardControls.ocx\n",
"Object={86CF1D34-0C5F-11D2-A9FC-0000F8754DA1}#2.0#0; mscomct2.ocx\n");

for (@objs)
{
chomp;
my ($crap, $guid, $ver, $rev, $name) = split /[=#;] ?/;

print "crap = $crap\n";
print "guid = $guid\n";
print "ver = $ver\n";
print "rev = $rev\n";
print "name = $name\n\n";
}
 
C

ChocMonk

Man, regexps always catch me out, no matter how much I practice them,
especially ones with spaces or multiline strings.
Ah well, thanks a lot, people.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top