CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [bill1234] Parsing data from a file with split & regex: Edit Log



7stud
Enthusiast

Jan 23, 2013, 4:14 PM


Views: 1171
Re: [bill1234] Parsing data from a file with split & regex


In Reply To
I have a file with large amounts of data, and it will be listed in one large text string for example:
employee name="Steve" employee phone="5551234123" employee address=''1234 street"

The first thing you should do is ring the neck of the person that chose that format.


Code
 
use strict;
use warnings;
use 5.012;

my $test = 'employee name="Steve" employee phone="5551234123" employee address="1234 street"';

while ($text =~ /
\s* #0 or more spaces
( #start of $1 (whole field)
( #start of $2 (field name)
[^=]+ #not an equals sign, 1 or more times
) #end of $2
= #an equals sign
" #a double quote
( #start of $3 (field value)
[^"]+ #not a double quote, 1 or more times
) #end of $3
" #a double quote
) #end of $1
/gxms #global matching flag plus standard xms
) {

my($whole_field, $field_name, $field_value) = ($1, $2, $3);
say $whole_field;
say $field_name;
say $field_value;
say '*' x 20;
}

--output:--
employee name="Steve"
employee name
Steve
********************
employee phone="5551234123"
employee phone
5551234123
********************
employee address="1234 street"
employee address
1234 street
********************



(This post was edited by 7stud on Jan 23, 2013, 9:35 PM)


Edit Log:
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 4:37 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 4:39 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 4:40 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 4:56 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 4:58 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 5:06 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 5:07 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 5:21 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 5:22 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 5:23 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 5:28 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 5:28 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 5:29 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 5:50 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 5:51 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 5:52 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 5:52 PM
Post edited by 7stud (Enthusiast) on Jan 23, 2013, 9:35 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives