contentline = name *(";" param ) ":" value CRLF name = iana-token / x-name iana-token = 1*(ALPHA / DIGIT / "-") ; registered with IANA x-name = "X-" 1*(ALPHA / DIGIT / "-") ; Reserved for non-standard use param = param-name "=" param-value *("," param-value) param-name = iana-token / x-name param-value = ptext / quoted-string ptext = *SAFE-CHAR value = *VALUE-CHAR quoted-string = DQUOTE QSAFE-CHAR DQUOTE NON-ASCII = %x80-FF QSAFE-CHAR = WSP / %x21 / %x23-7E / NON-ASCII SAFE-CHAR = WSP / %x21 / %x23-2B / %x2D-39 / %x3C-7E / NON-ASCII VALUE-CHAR = WSP / VCHAR / NON-ASCII WSP = %x20 / %x09 VCHAR = %x21-7E CRLF = %xOD %xOA DQUOTE = %x22 ALPHA = %x41-5A / %x61-7A DIGIT = %x30-39
(([A-Za-z0-9-]+|X-[A-Za-z0-9-]+)(;([A-Za-z0-9-]+|X-[A-Za-z0-9-]+)=([\s\x21\x23-\x2B\x2D-\x39\x3C-\x7E\x80-\xFF]*|\x22[\s\x21\x23-\x7E\x80-\xFF]*\x22)(,([\s\x21\x23-\x2B\x2D-\x39\x3C-\x7E\x80-\xFF]*|\x22[\s\x21\x23-\x7E\x80-\xFF]*\x22))*)*:([\s\x21-x7E\x80-\xFF]*))
TEL;TYPE=work;TYPE=voice;type=pref;TYPE=msg:+1-213-555-1234 TEL;TYPE=work,voice;type=pref;TYPE=msg:+1-213-555-1234
string line = @"(([A-Za-z0-9-]+|X-[A-Za-z0-9-]+)(;([A-Za-z0-9-]+|X-[A-Za-z0-9-]+)=([\s\x21\x23-\x2B\x2D-\x39\x3C-\x7E\x80-\xFF]*|\x22[\s\x21\x23-\x7E\x80-\xFF]*\x22)(,([\s\x21\x23-\x2B\x2D-\x39\x3C-\x7E\x80-\xFF]*|\x22[\s\x21\x23-\x7E\x80-\xFF]*\x22))*)*:([\s\x21-x7E\x80-\xFF]*))"; Regex regex = new Regex(line); Match match = regex.Match(@"TEL;TYPE=work,voice;type=pref;TYPE=msg:+1-213-555-1234"); if (match.Success && match.Index == 0) { Console.WriteLine(String.Format("name ={0}", match.Groups[2].Value)); Console.WriteLine(String.Format("param name ={0}", match.Groups[4].Value)); Console.Write( "param values="); foreach (Capture c in match.Groups[5].Captures) Console.Write(String.Format("{0} ", c.Value)); foreach (Capture c in match.Groups[7].Captures) Console.Write(String.Format("{0} ", c.Value)); Console.WriteLine(); Console.WriteLine(String.Format("value ={0}", match.Groups[8].Value)); }
name =TEL param name =TYPE param values=work pref msg voice value =+1-213-555-1234
public static class VCardParser { public static bool Parse(string field, ref string name, ref string paramName, ref string[] paramValues, ref string value) { bool result = false; Match match = m_fieldRegex.Match(field); if (match.Success && match.Index == 0) { name = match.Groups[2].Value; value = match.Groups[8].Value; paramName = match.Groups[4].Value; paramValues = new string[match.Groups[5].Length + match.Groups[7].Length]; int n = 0; foreach (Capture c in match.Groups[5].Captures) paramValues[n++] = c.Value; foreach (Capture c in match.Groups[7].Captures) paramValues[n++] = c.Value; result = true; } return(result); } private static string DQUOTE = @"\x22"; private static string IANA_TOKEN = @"[A-Za-z0-9-]+"; private static string X_NAME = @"X-[A-Za-z0-9-]+"; private static string SAFE_CHAR = @"[\s\x21\x23-\x2B\x2D-\x39\x3C-\x7E\x80-\xFF]"; private static string QSAFE_CHAR = @"[\s\x21\x23-\x7E\x80-\xFF]"; private static string VALUE_CHAR = @"[\s\x21-x7E\x80-\xFF]"; private static string NAME = IANA_TOKEN + "|" + X_NAME; private static string PARAM_NAME = NAME; private static string PARAM_VALUE = SAFE_CHAR + "*|" + DQUOTE + QSAFE_CHAR + "*" + DQUOTE; private static string CONTENT_LINE = "(" + // group 1 "(" + NAME + ")" + // group 2 "(;" + // group 3 "(" + PARAM_NAME + ")=" + // group 4 "(" + PARAM_VALUE + ")" + // group 5 "(," + // group 6 "(" + PARAM_VALUE + ")" + // group 7 ")*" + ")*" + ":(" + VALUE_CHAR + "*)" + // group 8 ")"; private static Regex m_fieldRegex = new Regex(CONTENT_LINE); }
string name = null; string value = null; string paramName = null; string[] paramValues = null; if (VCardParser.Parse(@"TEL;TYPE=work,voice;type=pref;TYPE=msg:+1-213-555-1234", ref name, ref paramName, ref paramValues, ref value)) { Console.WriteLine(String.Format("name ={0}", name)); Console.WriteLine(String.Format("param name ={0}", paramName)); Console.Write( "param values="); foreach (string s in paramValues) Console.Write(String.Format("{0} ", s)); Console.WriteLine(); Console.WriteLine(String.Format("value ={0}", value)); }
PHOTO;ENCODING=b;TYPE=JPEG:MIICajCCAdOgAwIBAgICBEUwDQYJKoZIhvcN AQEEBQAwdzELMAkGA1UEBhMCVVMxLDAqBgNVBAoTI05ldHNjYXBlIENvbW11bm ljYXRpb25zIENvcnBvcmF0aW9uMRwwGgYDVQQLExNJbmZvcm1hdGlvbiBTeXN0 (...remainder of "B" encoded binary data...)
Labels: .NET, C#, regex, regex match, regex tool, regular expressions, vcard, vcard format, vcf, vcf format