Tanoti has asked for the wisdom of the Perl Monks concerning the following question:
I have some text from a third-party app which I'm storing in a single variable that needs parsing. I am using:
split /\n/, $app_text;
to break it into lines for processing. I'm looking for "Field:Value" lines and ignoring everything else. For most of the text this is fine, however the external app is borking some of the fields and putting a \n after the colon meaning the Value for that field ends up in the next array element. Here's some sample code:
Produces:
How can I tell split to split on the \n but not if preceeded by a colon, so I get two:turtle doves for the second array element in the above example?
Many thanks,
John
#!/usr/bin/perl use strict; my $app_text = "one:partridge\ntwo:\nturtle doves\nthree:french hens\n +"; foreach my $line (split /\n/, $app_text) { print "$line\n"; }
Produces:
one:partridge two: turtle doves three:french hens
How can I tell split to split on the \n but not if preceeded by a colon, so I get two:turtle doves for the second array element in the above example?
Many thanks,
John
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Pattern match for split() - need match but not match syntax
by citromatik (Curate) on May 06, 2008 at 14:11 UTC | |
by Tanoti (Initiate) on May 06, 2008 at 14:52 UTC | |
Re: Pattern match for split() - need match but not match syntax
by Narveson (Chaplain) on May 06, 2008 at 14:59 UTC | |
by johngg (Canon) on May 06, 2008 at 18:15 UTC | |
Re: Pattern match for split() - need match but not match syntax
by GrandFather (Saint) on May 06, 2008 at 22:23 UTC |
Back to
Seekers of Perl Wisdom