I don't think the trim leading and trailing spaces statements are needed here given your DATA. However, you should become familiar with how to do that.#!/usr/bin/perl use strict; use warnings; while (my $line = <DATA>) { next if $line =~ /^\s*$/; # skip blank lines $line =~ s/^\s*//; # remove leading spaces $line =~ s/\s*$//; # remove trailing space and line ending + my ($name, @nums) = split /[\s-]+/, $line; foreach my $num (@nums) { print "$name\t$num\n"; } } # PRINTS #abcd 723 #abcd 724 #abcde 552 #abcde 554 #abcde 553 #abcdef 756 __DATA__ abcd 723-724 abcde 552-554-553 abcdef 756
Update: As a general rule:
I almost always have a statement to throw away blank lines. They can often appear at the end of a file and hard to see when you just type or cat the file.#!/usr/bin/perl use strict; use warnings; while (my $line = <DATA>) { next if $line =~ /^\s*$/; # skip blank lines my ($name, @nums) = $line =~ /(\w+)/g; foreach my $num (@nums) { print "$name\t$num\n"; } } # PRINTS #abcd 723 #abcd 724 #abcde 552 #abcde 554 #abcde 553 #abcdef 756 __DATA__ abcd 723-724 abcde 552-554-553 abcdef 756
In reply to Re: regex problem
by Marshall
in thread regex problem
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |