in reply to Extracting multiple match and reorganizing order with Regex

There are quite a few assumptions being made so it leads to excess code, especially since I don't have much time to really focus and golf it down, but give this a whirl:

use warnings; use strict; my $line1 = "Positive for Depression ( mother ; sister ), Type 2 Di +abetes ( father ; mother ; grandparents ) and Anxiety ( mother) ." +; my $line2 = "Cancer ( mother, grandmother )"; my %issues; for my $line (($line1, $line2)){ $line =~ s/(?:Positive for | and )//g; $line =~ s/,/;/g; # remove a semi-colon, if it immediately follows a closing # parens. This ; is the comma after "; sister )" that we # turned into a semi-colon $line =~ s/(?<=\));//g; if (my @entries = $line =~ /.*?\(.*?\)/g){ for (@entries){ /(.*)\((.*)\)/; my $issue = $1; my $people = $2; $issue =~ s/^\s+//g; # squash leading whitespace $people =~ s/\s+//g; # squash all whitespace @{ $issues{$issue} } = split /;/, $people; } } } for my $issue (keys %issues){ for my $person (@{ $issues{$issue} }){ print "$person || $issue\n"; } } __END__ mother || Cancer grandmother || Cancer father || Type 2 Diabetes mother || Type 2 Diabetes grandparents || Type 2 Diabetes mother || Depression sister || Depression mother || Anxiety