in reply to Extracting multiple match and reorganizing order with Regex
Script:
#!/usr/bin/env perl use strict; use warnings; use diagnostics; use feature 'say'; while (<DATA>) { chomp; if ($_ ne q{}) { s/Positive\sfor\s//; while (/ (?:and\s)? ((?:\w+\s)+) # Remember disease name \( ([^\)]+) # Remember list of family members \) /xg) { my ($disease, @persons) = ($1, map { s/^\s+//; s/\s+$//; $_; } split /;|,/, $2); $disease =~ s/\s$//; foreach my $person (@persons) { say "$person || $disease"; } } } } close DATA or die 'Could not close DATA: ', $!; __DATA__ Positive for Depression ( mother ; sister ), Type 2 Diabetes ( fat +her ; mother ; grandparents ) and Anxiety ( mother) . Cancer ( mother, grandmother )
Output:
mother || Depression sister || Depression father || Type 2 Diabetes mother || Type 2 Diabetes grandparents || Type 2 Diabetes mother || Anxiety mother || Cancer grandmother || Cancer
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Extracting multiple match and reorganizing order with Regex
by NewMonk2Perl (Sexton) on Apr 19, 2016 at 15:52 UTC | |
by stevieb (Canon) on Apr 19, 2016 at 16:13 UTC |