Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl regex split with new lines

Tags:

regex

split

perl

I'm new to Perl and am working on a project for school and am stuck.

Input: A given text file containing email addresses delimited by a space, tab, ",", ";" or “:” [can be on separate lines].

I am trying to read in the email addresses and put them into an array. I am able to parse the data on one line however if there are line breaks or returns I only get the last element.

Can someone help me figure out how to take a list with each address on a separate line and parse them? I have read a bit on regex but need much more practice. Thanks.

open(EmailAddresses, "EmailAdressesCommaList.txt") || die "Can not open file $!";

# 
while (<EmailAddresses>)
{
    chomp;
    # Split the line into words
    @lines = split /[ ,;:\t\r\n(\t\r\n\s)+?]/;
}

foreach $value (@lines)
{
    print $value . "\n";
}

2 Answers

open(EmailAddresses, "EmailAdressesCommaList.txt") || die "Can not open file $!";
while(<EmailAddresses>) {
    chomp;
    push @lines, split /[ ,;:\t\r\n(\t\r\n\s)+?]/;
}
foreach $value (@lines) {
    print $value . "\n";
}

i.e. the problem isn't your regex, it's that you're overwriting @lines each time through the loop.

like image 85
chaos Avatar answered Dec 07 '25 20:12

chaos


As chaos pointed out, you should push onto the array, not overwrite it, but your regex is strange, too. It seems that you want to do:

/[ ,;:\t\r\n][\t\r\n\s]+/

However, I think that this will work as well:

/[,;:\s]+/
like image 28
Svante Avatar answered Dec 07 '25 22:12

Svante



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!