How can I extract multiple email addresses from a single line using a Perl regex?

Use the g modifier to match a regular expression multiple times.

Use the /g modifier to match a regular expression multiple times: open (FP, ') { my @matches = /(\S{1,80}@\S{2,100})\s/g; push @emails, @matches; } close (FP).

Sounds like you're happy with the regex but just need to capture multiple matches. To do that, you need to stop using if and use the /g modifier. Additionally, if the file is small enough, you could then read the whole file into a single string since you are able to deal with multiple matches now.

Here is an example: use strict; use warnings; open my $fh, ' }; # read the whole file my @emails; push @emails, $contents =~ /emailpattern/g; The matches are returned in the array context, so don't need capturing parens or $1 either.

Assuming your regex itself is correct, to match more than once, place the g modifier at the end to match multiple times, and then return the list. Your code then looks something like this: open (FP, ') { push @emails, $line =~ /your_regex_here/g; } close (FP).

Its also safer to use lexical file handles if your script grows larger: open(my $fp, ...) – Eric Strom Dec 28 '09 at 16:30.

Assuming email addr are one word by itself separated by spaces, no need for too much regex while (){ chomp; @s = split /\s+/ ; foreach my $i (@s){ print $i . "\n" if $i =~ /@/; } }.

Ivan Nevostruev's answer has flaws. #! /usr/bin/perl open (FP, ') { my @matches = /(\S{1,80}@\S{2,100})\s/g; push @emails, @matches; } close (FP); This will not work because @matches is matching against $_, which will not exist because is being read into $line.

Also, imho, creating two array symbols (one from the other) seems poorly implemented. It seems better to me to create one symbol from the resulting list and that one symbol being the container for all of the data you need. See below for my example.

* Here's a nice link for an email regex ex-parrot.com/pdw/Mail-RFC822-Address.html. I recommend working it in something like this (over-kill? Yeah, probably (:): #!

/usr/bin/perl -w use strict; open(F,'@,;:\\". \\ \000-\031+(?:(? :(?

:\r\n)? \t'.')+|\Z|(?=\"()@,;:\\". \\))|"(?:^\"\r\\|\\.

|(?:(? :\r\n)? \t))*"(?:(?

:'. '\r\n)? \t)*)(?:\.(?:(?

:\r\n)? \t)*(?:^()@,;:\\". \\ \000-\031+(?:(?

:('. '? :\r\n)?

\t)+|\Z|(?=\"()@,;:\\". \\))|"(?:^\"\r\\|\\. |(?:(?

:\r\n)? '. '\t))*"(?:(?

:\r\n)? \t)*))*@(?:(? :\r\n)?

\t)*(?:^()@,;:\\". \\ \000-\0'.'31+(?:(? :(?

:\r\n)? \t)+|\Z|(?=\"()@,;:\\". \\))|\(^\\\r\\|\\.)*\'.

'(?:(? :\r\n)? \t)*)(?:\.(?:(?

:\r\n)? \t)*(?:^()@,;:\\". \\ \000-\031+'.

'(?:(? :(? :\r\n)?

\t)+|\Z|(?=\"()@,;:\\". \\))|\(^\\\r\\|\\. )*\(?:'.'(?:\r\n)?

\t)*))*|(?:^()@,;:\\". \\ \000-\031+(?:(? :(?

:\r\n)? \t)+|\Z'. '|(?=\"()@,;:\\".

\\))|"(?:^\"\r\\|\\. |(?:(? :\r\n)?

\t))*"(?:(? :\r\n)'. '?

\t)*)*\(?:(? :\r\n)? \t)*)|(?:^()@,;:\\".

\\ \000-\031+(?:(?'.':(? :\r\n)? \t)+|\Z|(?=\"()@,;:\\".

\\))|"(?:^\"\r\\|\\. |(?:(? :\r\n)?

'. ' \t))*"(?:(? :\r\n)?

\t)*)*:(? :(? :\r\n)?

\t)*(?:(? :(? :^()@,;:\\".

\\ '.'\000-\031+(?:(? :(? :\r\n)?

\t)+|\Z|(?=\"()@,;:\\". \\))|"(?:^\"\r\\|'.'\\. |(?:(?

:\r\n)? \t))*"(?:(? :\r\n)?

\t)*)(?:\.(?:(? :\r\n)? \t)*(?:^()'.

'@,;:\\". \\ \000-\031+(?:(? :(?

:\r\n)? \t)+|\Z|(?=\"()@,;:\\". \\))|"'.'(?:^\"\r\\|\\.

|(?:(? :\r\n)? \t))*"(?:(?

:\r\n)? \t)*))*@(?:(? :\r\n)?

\t'. ')*(?:^()@,;:\\". \\ \000-\031+(?:(?

:(? :\r\n)? \t)+|\Z|(?=\"()@,;:\\'.

'". \\))|\(^\\\r\\|\\. )*\(?:(?

:\r\n)? \t)*)(?:\.(?:(? :\r\n)?

\t)*(?'. ':^()@,;:\\". \\ \000-\031+(?:(?

:(? :\r\n)? \t)+|\Z|(?=\"()@,;:\\".

\'. '\))|\(^\\\r\\|\\.)*\(?:(? :\r\n)?

\t)*))*|(?:^()@,;:\\". \\ \000-'.'\031+(?:(? :(?

:\r\n)? \t)+|\Z|(?=\"()@,;:\\". \\))|"(?:^\"\r\\|\\.

|('. '? :(?

:\r\n)? \t))*"(?:(? :\r\n)?

\t)*)*\(?:(? :\r\n)? \t)*)(?:,\s*('.'?

:(? :^()@,;:\\". \\ \000-\031+(?:(?

:(? :\r\n)? \t)+|\Z|(?=\"()@,;:\\'.

'". \\))|"(?:^\"\r\\|\\. |(?:(?

:\r\n)? \t))*"(?:(? :\r\n)?

\t)*)(?:\.(?:('.'? :\r\n)? \t)*(?:^()@,;:\\".

\\ \000-\031+(?:(? :(? :\r\n)?

\t)+|\Z|(?='. '\"()@,;:\\". \\))|"(?:^\"\r\\|\\.

|(?:(? :\r\n)? \t))*"(?:(?

:\r\n)? \t'. ')*))*@(?:(?

:\r\n)? \t)*(?:^()@,;:\\". \\ \000-\031+(?:(?

:(? :\r\n)? \t'.

')+|\Z|(?=\"()@,;:\\". \\))|\(^\\\r\\|\\. )*\(?:(?

:\r\n)? \t)*)(?'.':\.(?:(? :\r\n)?

\t)*(?:^()@,;:\\". \\ \000-\031+(?:(? :(?

:\r\n)? \t)+|'. '\Z|(?=\"()@,;:\\".

\\))|\(^\\\r\\|\\.)*\(?:(? :\r\n)? \t)*))*|(?:'.

'^()@,;:\\". \\ \000-\031+(?:(? :(?

:\r\n)? \t)+|\Z|(?=\"()@,;:\\". \\'.'))|"(?:^\"\r\\|\\.

|(?:(? :\r\n)? \t))*"(?:(?

:\r\n)? \t)*)*\(?:('. '?

:\r\n)? \t)*))*)? ;\s*)/mig'; my @all; while ( chomp(my $line = ) ) { push(@all,$line =~ qr($re)); } close(F).

I think one of posible falure is using \s instead of \b. Try following condition: if($line =~ /\b(\S{1,80}@{1}\S{2,100})\b/) Also always check if file is opened successful. And use strict/warnings: use strict; use warnings; open (FP, 'Txt') or die "can't open file: $!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions