vCardを解析するための次の正規表現があります:(VB)
Dim options As New RegexOptions()
options = RegexOptions.IgnoreCase Or RegexOptions.Multiline Or RegexOptions.IgnorePatternWhitespace
regex = New Regex("(?<strElement>(N)) (;[^:]*)? (;CHARSET=UTF-8)? (:(?<strSurname>([^;\n\r]*))) (;(?<strGivenName>([^;\n\r]*)))? (;(?<strMidName>([^;\n\r]*)))? (;(?<strPrefix>([^;\n\r]*)))? (;(?<strSuffix>[^;\n\r]*))?", options)
m = regex.Match(s)
If m.Success Then
Surname = m.Groups("strSurname").Value
GivenName = m.Groups("strGivenName").Value
MiddleName = m.Groups("strMidName").Value
Prefix = m.Groups("strPrefix").Value
Suffix = m.Groups("strSuffix").Value
End If
次のような vCard がある場合に機能します。
BEGIN:VCARD
VERSION:2.1
N:Bacon;Kevin;Francis;Mr.;Jr.
FN: Mr. Kevin Francis Bacon Jr.
ORG:Movies.com
ただし、vCard が次のような場合は正しく機能しません。
BEGIN:VCARD
VERSION:2.1
N:Bacon;Kevin
FN:Kevin Bacon
ORG:Movies.com
正規表現は <strSuffix> を Kevin に割り当てますが、私が望んでいた <strGivenName> には割り当てません。どうすればこれを修正できますか?
適応された正規表現はここから来ました: vCard regex