Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Parsing semi-erratic text

by ikegami (Patriarch)
on Aug 23, 2006 at 19:39 UTC ( #569191=note: print w/replies, xml ) Need Help??

in reply to Parsing semi-erratic text

The data you submitted *does* have newlines.

while (<DATA>) { my ($key, $val) = /^\s*([^:]*?)\s*:\s*(.*?)\s*$/ or next; print("[$key:$val]\n"); }


while (<DATA>) { my ($key, $val) = split(/:/, $_, 2); next if not defined $val; s/^\s+//, s/\s+$// for $key, $val; print("[$key:$val]\n"); }

both do the trick.

Replies are listed 'Best First'.
Re^2: Parsing semi-erratic text
by SamCG (Hermit) on Aug 23, 2006 at 19:50 UTC
    Hrmm...perhaps an effect of my cutting and pasting? The body of my email gets read into a variable (so it's like slurping a file). I can't seem to split on newlines, and if I use a regex to count I get only one in each email (which I presume is at the end).

    Thank you for the implicit character class ([^:]) suggestion, by the way. I hate putting .* into regexes, even with the non-greedy modifier.

    s''limp';@p=split '!','n!h!p!';s,m,s,;$s=y;$c=slice @p1;so brutally;d;$n=reverse;$c=$s**$#p;print(''.$c^chop($n))while($c/=$#p)>=1;
      I agree. .* and .*? usually/often assume the data is formatted correctly.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://569191]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-05-27 05:55 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (94 votes). Check out past polls.