On Sat, 22 Dec 2012 16:45:21 +0530
punit jain <contactpunitj...@gmail.com> wrote:

> Hi,
> 
> I have a file like below : -

[snipped example - vcards with mail headers etc in between]


> My requirement is to get all text between BEGIN:VCARD and END:VCARD
> and all the instances. So o/p should be :-
[...]
> I am using below regex  :-
[...]
> Any suggestions ?

You've already had a reply indicating how to solve the problem you were
having with regexes, so I won't touch on that.

What I will advise, is that for any task you're trying to accomplish,
there's a pretty good chance someone has already solved that and made
code available on CPAN that will help you - so always check CPAN first,
to avoid unnecessarily reinventing the wheel each time (unless you're
doing so solely for a learning experience, of course).

In this case, parsing vcards is likely a common task - a quick look on
CPAN turns up Text::vCard::Addressbook:

https://metacpan.org/module/Text::vCard::Addressbook


From the synopsis:

  use Text::vCard::Addressbook;
 
  my $address_book = Text::vCard::Addressbook->new(
      { 'source_file' => '/path/to/address.vcf', } );
 
  foreach my $vcard ( $address_book->vcards() ) {
      print "Got card for " . $vcard->fullname() . "\n";
  }

It will ignore the non-vcard content in the example you provided, and
just provide you easy access to the data from each vcard.

That's a much nicer approach than extracting it yourself with regexes.

Cheers

Dave P


-- 
David Precious ("bigpresh") <dav...@preshweb.co.uk>
http://www.preshweb.co.uk/     www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedin    www.preshweb.co.uk/facebook
www.preshweb.co.uk/cpan        www.preshweb.co.uk/github



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to