Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Think about Loose Coupling
 
PerlMonks  

Re: Oft encountered regex problem

by dbwiz (Curate)
on Jul 18, 2003 at 18:14 UTC ( [id://275778]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Oft encountered regex problem

This should do what you want.

#!/usr/bin/perl -w use strict; use Data::Dumper; my $first = <<'END'; name is Doug eyes: brown email at bill@hotmail.com END my $second = <<'END'; name is Fred eyes: black era: prehistoric email at fflinstone@hotmail.com END sub parse { my $text = shift; my %info = (); while ($text =~ / ^ # start of line (\w+) # a word [:\s] # colon or space .* # anything \s # a space (\S+) # a sequence of non spaces $ # till the end of line /xmg) { $info{$1} = $2; } return \%info; } for ($first, $second) { print Data::Dumper->Dump([parse($_)], ['info']),"\n"; } __END__ $info = { 'eyes' => 'brown', 'email' => 'bill@hotmail.com', 'name' => 'Doug' }; $info = { 'eyes' => 'black', 'era' => 'prehistoric', 'email' => 'fflinstone@hotmail.com', 'name' => 'Fred' };

You can assign the result from parse() to a hash reference and eventually build the structure that suits your needs best.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://275778]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.