'How to split a vcard file into separate contacts with regex in Python?
I'm working on VCF files for some purpose. I got a whole collection of contacts in a single VCF file. But I want to extract each contacts separately from the file. I want to use regex to make it simple. How can I achieve it?
I have make a pattern but it is matching all contacts at once.
import re
a = \
'''BEGIN:VCARD
VERSION:3.0
N:-Achham;Bhaskar;Saud;;
FN:Bhaskar -Achham
NOTE:
TEL;TYPE=CELL;TYPE=pref;TYPE=VOICE:9741062727
END:VCARD
BEGIN:VCARD
VERSION:3.0
FN:Rohit Joshi
N:;Rohit Joshi;;;
END:VCARD'''
pattern = 'BEGIN:VCARD.*END:VCARD'
match = re.findall(pattern,a,re.DOTALL)
print(match)
I was expecting a list of each contacts but the result I am getting is this.
['BEGIN:VCARD\nVERSION:3.0\nN:-Achham;Bhaskar;Saud;;\nFN:Bhaskar -Achham\nNOTE:\nTEL;TYPE=CELL;TYPE=pref;TYPE=VOICE:9741062727\nEND:VCARD\nBEGIN:VCARD\nVERSION:3.0\nFN:Rohit Joshi\nN:;Rohit Joshi;;;\nEND:VCARD']
Solution 1:[1]
You have to modify your regex to do a non-greedy match .*?
.
pattern = r'(?s)BEGIN:VCARD.*?END:VCARD'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |