Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: parsing a line with $1, $2, $3

by CountZero (Bishop)
on Mar 10, 2012 at 21:34 UTC ( #958898=note: print w/replies, xml ) Need Help??


in reply to parsing a line with $1, $2, $3

I am not sure I understood fully your requirement, but here is my best try. It is not in one regex, but a combination of a split, a regex and a substitution.
use Modern::Perl; use Data::Dump qw /dump/; while (<DATA>) { chomp; my ($namedosageform, $instructions, $code) = grep {/\S/} split /<. +*?>/; my ($name, $dosage, $form) = $namedosageform =~ /^([^\d]+)(\d+[^,] ++),\s*(.*)$/; $code =~ s/mfg codes:(.*)/$1/; say "Name: $name\nDosage: $dosage\nForm: $form\nInstructions: $ins +tructions\nMFG codes: $code\n"; } __DATA__ <h3>FENTANYL 25MCG/HR PATCH, TRANSDERMAL 72 HOURS</h3> <p class='cd1'> +Restricted to NDC labeler code 50458 (Janssen) and to a maximum of te +n (10) transdermal patches per dispensing and a maximum of three (3) +dispensings of any strength in a 75-day period only. </p><p>mfg codes +:50458</p><p>DURAGESIC</p> <h3>ACETAMINOPHEN 80MG/0.8ML SUSPENSION, DROPS(FINAL DOSAGE FORM)(ML)< +/h3> <p class='cd1'>Restricted to individuals younger than 21 years o +f age for the liquid and drops only. </p><p>mfg codes:68016, 63868, 6 +3162, 49348, 46122, 36800, 00904, 00536, 00472, 00113, 00067</p><p>TR +IAMINIC FEVER REDUCER | PAIN & FEVER | NORTEMP | MAPAP INFANT | INFAN +TS' NON-ASPIRIN | INFANT'S PAIN RELIEVER | INFANT'S PAIN RELIEF | INF +ANT'S NON-ASPIRIN | ACETAMINOPHEN</p>
Output:
Name: FENTANYL 
Dosage: 25MCG/HR PATCH
Form: TRANSDERMAL 72 HOURS
Instructions: Restricted to NDC labeler code 50458 (Janssen) and to a maximum of ten (10) transdermal patches per dispensing and a maximum of three (3) dispensings of any strength in a 75-day period only. 
MFG codes: 50458

Name: ACETAMINOPHEN 
Dosage: 80MG/0.8ML SUSPENSION
Form: DROPS(FINAL DOSAGE FORM)(ML)
Instructions: Restricted to individuals younger than 21 years of age for the liquid and drops only. 
MFG codes: 68016, 63868, 63162, 49348, 46122, 36800, 00904, 00536, 00472, 00113, 00067

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://958898]
help
Chatterbox?
[msh210]: I think I just found a lie in perldoc.
[msh210]: warn: "If the last element of LIST does not end in a newline, it appends the same file/line number text as die does."
[msh210]: perl -e'warn "test$/", "" '
[msh210]: doesn't append anything.
[msh210]: It should say "If the last nonempty element of LIST" or "If the string thus to be printed does not end in a newline""
[msh210]: Am I wrong?
[shmem]: no. You're right.
[msh210]: About time... last time I was right was... I don't even remember when.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2017-04-24 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (439 votes). Check out past polls.