2012-11-15 47 views
1

我想讀取VCard文件。如何閱讀VCard文件?

我使用this樣本。

但是當我使用這個解決方案這個文件。

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 

找不到電子郵件/電話/地址的參數。

例如我用這個正則表達式的電話

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); 

但strAttr的價值,strType空。

如何爲這些設置正則表達式?

回答

1

我用這個正則表達式地址:

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

你使用哪些選項?

如果您不使用IgnorePatternWhitespace,則表達式中的空格是個問題。

的另一個問題是,你檢查的「聲音」前;,但在你的字符串有一個,

你真的需要所有這些*量詞?如果你想做一些可選的事情,最好使用?量詞,它只匹配0或1次。

+0

謝謝,我想分組串兩(類型,值)。當我使用這個正則表達式,我有「:電話:+ 1-404-555-1212」爲strValue但strAttr是空的。 strAttr我可以獲得「家,聲音」。 – Niloo

1

你可以嘗試找到一個現有的庫,而不是重新發明輪子。 This for instance