[lug] Perl regex question

Zan Lynx zlynx at acm.org
Wed Sep 20 13:52:47 MDT 2006


On Wed, 2006-09-20 at 13:16 -0600, Bill Thoen wrote:
> If I have strings like the following:
> A,2, 3, 5, 7
> F, 8, 9, 1
> 
> and I want to turn them into stirngs like these:
> A2 A3 A5 A7
> F8 F9 F1
> 
> using Perl regex, how is this done?  I'm trying something like the 
> following:
> #!/usr/bin/perl
> while (<>) {
>   chop;
>   s/^[A-Z][, ]+(([0-9])[, ]+[^, ])+/$1$2 /g;
>   print "$_\n";
> }
> 
> But of course, it doesn't work. How does one process a list of items 
> like this using Perl's regular expressions?

Don't try doing everything with regex.  It's a good tool, but very messy
for some things.

I would just do this (warning, untested):
@list = split(',', $string);
$letter = shift @list;

$newstring = join(' ', map { $letter.$_ } @list);

-- 
Zan Lynx <zlynx at acm.org>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20060920/041f432e/attachment.pgp>


More information about the LUG mailing list