Deb wrote: > > Hi, > > I'm having trouble coming up with a regular expression for $lines in @lines > of this form: > > units = [EMAIL PROTECTED] [EMAIL PROTECTED]:[EMAIL PROTECTED] > units = [EMAIL PROTECTED] > units = [EMAIL PROTECTED] [EMAIL PROTECTED] > > ' ^units = ' is expected, all the rest is not - ie, one or more addrs, but > they must be comma separated, not space, semi-colon or colon separated. So > far, I've got: > > foreach my $line (@lines) { > if ($line =~ /^units/) > { > $line =~ s/:/,/g; > $line =~ s/;/,/g; > } > } > > The part I'm having trouble with is replacing the space(s) between the > addresses. I don't want to touch any other whitespace. Only the space or > spaces which might separate more than one address. It's okay if there's a > comma and a space, but anything else needs to be replaced with a comma. There > also doesn't have to be a space after the comma. Only the comma is necessary. > > I can't seem to find the right incantation to replace just those spaces.
$ perl -e' my @lines = ( q/units = [EMAIL PROTECTED] [EMAIL PROTECTED]:[EMAIL PROTECTED]/, q/units = [EMAIL PROTECTED] /, q/units = [EMAIL PROTECTED] [EMAIL PROTECTED]/, ); for my $line ( @lines ) { print "$line\n"; $line =~ s{^(units\s+=\s+)(\S.*\S)} { ( $a, $b ) = ( $1, $2 ); $b =~ s/\b(?:(?<!,) +|[;:])\b/, /g; "$a$b" }e; print "$line\n"; } ' units = [EMAIL PROTECTED] [EMAIL PROTECTED]:[EMAIL PROTECTED] units = [EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED] units = [EMAIL PROTECTED] units = [EMAIL PROTECTED] units = [EMAIL PROTECTED] [EMAIL PROTECTED] units = [EMAIL PROTECTED], [EMAIL PROTECTED] John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]