Shrikage: regexp rookie

D

Don Stefani

Hello,
I'm cleaning up this data string before it's written to a flat file.
I'd like to know if/how I can shorten this regexp, so it's sexier. ;-)
Chick's dig sexy code!

Original line:
Color: R14/88H Golden Wheat<br>Size: Average<br>

My regexp:
$ary2[2] =~ s/(Color:|Size:)//g;
$ary2[2] =~ s/(\<br\>)/,/g;
$ary2[2] =~ s/(^\s+)//;

Thanks,

Don
 
G

Gunnar Hjalmarsson

Don said:
I'd like to know if/how I can shorten this regexp, so it's sexier.
;-) Chick's dig sexy code!

Original line:
Color: R14/88H Golden Wheat<br>Size: Average<br>

My regexp:
$ary2[2] =~ s/(Color:|Size:)//g;
$ary2[2] =~ s/(\<br\>)/,/g;
$ary2[2] =~ s/(^\s+)//;

Not sure about your definition of 'sexy', but...

$ary2[2] = "$1,$2" if $ary2[2] =~ /:\s+(.+?)<.+?:\s+([^<]+)/;
 
U

Uri Guttman

GH" == Gunnar Hjalmarsson said:
Don said:
I'd like to know if/how I can shorten this regexp, so it's sexier.
;-) Chick's dig sexy code!
Original line:
Color: R14/88H Golden Wheat<br>Size: Average<br>
My regexp:
$ary2[2] =~ s/(Color:|Size:)//g;
$ary2[2] =~ s/(\<br\>)/,/g;
$ary2[2] =~ s/(^\s+)//;

that doesn't need the () as you don't grab or group anything.
Not sure about your definition of 'sexy', but...
$ary2[2] = "$1,$2" if $ary2[2] =~ /:\s+(.+?)<.+?:\s+([^<]+)/;

blech!!

<all untested>

why not just use s///?

$ary2[2] =~ s/:\s+(.+?)<.+?:\s+([^<]+)/$1,$2/ ;

and that can be cleaned up too:

$ary2[2] =~ s/:\s+(.+?)<.+?:\s+(.+?)</$1,$2/ ;

now remove the regex redundancy:

$ary2[2] = join( ',', $ary2[2] =~ /:\s+(.+?)</g ) ;

and then remove the var redundancy:

$_ = join( ',', /:\s+(.+?)</g ) for $ary2[2] ;

bug alert: gunnar's and my code didn't replace the last <br> with , but
that is easily corrected.

now, that will get you the girl!

uri
 
M

Michele Dondi

I'd like to know if/how I can shorten this regexp, so it's sexier. ;-) [snip]
My regexp:
$ary2[2] =~ s/(Color:|Size:)//g;
$ary2[2] =~ s/(\<br\>)/,/g;
$ary2[2] =~ s/(^\s+)//;

Others have suggested some WTDI in one regex. To me it is sexier if it
is more simple and I find it easier to understand and possibly
maintain if three regex are used as you did. Only, do yourself a favor
and do not use three =~; take an advantage of foreach aliasing
instead: it is even in the faq IIRC! Try this (untested),

s/(?:Color|Size)://g, s/<br>/,/g, s/^\s*// for $ary2[2];


HTH,
Michele
 

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

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top