Niloo
Niloo

Reputation: 1215

How to read VCard file?

I want to read VCard file .

I use this sample.

but when i use this solution for this file .

BEGIN:VCARD
VERSION:4.0
N:Gump;Forrest;;;
FN: Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
PHOTO:http://www.example.com/dir_photos/my_photo.gif
TEL;TYPE=work,voice;VALUE=uri:tel:+1-111-555-1212
TEL;TYPE=home,voice;VALUE=uri:tel:+1-404-555-1212
ADR;TYPE=work;LABEL="42 Plantation St.\nBaytown, LA 30314\nUnited States of America"
 :;;42 Plantation St.;Baytown;LA;30314;United States of America
EMAIL:[email protected]
REV:20080424T195243Z
END:VCARD

don't find parametr of Email/Phone/Address.

for example i use this Regex for phone

   RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace;
    regex = new Regex(@"(\n(?<strElement>(TEL)) (;*(?<strAttr>(HOME|WORK)))* (;(?<strType>(VOICE|CELL|PAGER|MSG|FAX)))*  (;(?<strPref>(PREF)))* (;[^:]*)*  (:(?<strValue>[^\n\r]*)))", options);

but value of strAttr,strType in empty.

How to set Regex for these?

Upvotes: 1

Views: 3618

Answers (3)

Niloo
Niloo

Reputation: 1215

I use this regex for Address:

   regex = new Regex(@"(?<strElement>(ADR)) (;(?<strAttr>[^\n\r]*))? (:(?<strPo>([^;]*)))  (;(?<strBlock>([^;]*)))  (;(?<strStreet>([^;]*)))  (;(?<strCity>([^;]*))) (;(?<strRegion>([^;]*))) (;(?<strPostcode>([^;]*)))(;(?<strNation>[^\n\r]*))", options);

Upvotes: 1

Henrik Gering
Henrik Gering

Reputation: 1891

You could try to find an existing library instead of reinventing the wheel. This for instance.

Upvotes: 1

stema
stema

Reputation: 93046

What options do you use?

If you don't use IgnorePatternWhitespace, then the spaces in your expression are a problem.

Another problem is, you check for a ; before "VOICE", but in your string there is a ,

Do you really need all those * quantifiers? If you want to make something optional, it is better to use the ? quantifier, it matches only 0 or 1 times.

Upvotes: 0

Related Questions