2017-01-15 95 views
-3

我需要解析字符串,可以是下列条件之一:提取证书名称

"CN=bbb1, OU=b, O=b, L=b, S=b, C=US"  //expected result bbb1 
"CN=*.host.com, OU=b, O=b, L=b, S=b, C=US" //expected result *.host.com 
"CN = *.host.com "       //expected result *.host.com 
"CN = bbb1 "        //expected result bbb1 

我写了下面的解析函数:

public static string GetCertificateName(string certSubject) 
    { 
     System.Text.RegularExpressions.Regex regex; 
     try 
     { 
      regex = new System.Text.RegularExpressions.Regex(@"CN\s*=\s*(?<name>\w+)"); 
      var match = regex.Match(certSubject); 
      return match.Groups["name"].Value; 
     } 
     catch(Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 

    return "Can't parse"; 
} 
static void Main(string[] args) 
{ 
    Console.WriteLine(GetCertificateName("CN=bbb1, OU=b, O=b, L=b, S=b, C=US")); //bbb1 
    Console.WriteLine(GetCertificateName("CN = bbb3 "));//bb33 
    Console.WriteLine(GetCertificateName("CN = bbb4\n\t "));//bbb4 
    Console.WriteLine(GetCertificateName("CN=*.host.com"));//empty string!!! != *.host.com 
    Console.ReadLine(); 
} 

请帮我以提高我的解析功能,所以GetCertificateName(“CN = *。host.com”)调用将返回* .host.com 非常感谢, 娜塔莉

+2

'请帮我improve'什么需要改进,到底是什么? – Abion47

+0

'\ w +'不匹配'*'。将其更改为'\ S +'或'[\ w *] +',它应该可以工作。 –

+0

选中此:https://regex101.com/r/LsTjYb/3 – MYGz

回答

1

如果CN是字符串的开始(我们可以在所有的例子中看到),你可以尝试的LINQ液(开始于=,停在,,修整,中间)

string source = "CN = *.host.com "; 

    string result = string.Concat(source 
     .SkipWhile(c => c != '=') 
     .Skip(1) 
     .TakeWhile(c => c != ',')) 
    .Trim(); 

甚至好老IndexOfSubstring

string source = "CN =*.host.com,"; 

    int start = source.IndexOf('=') + 1; 
    int stop = start <= 0 ? - 1 : source.IndexOf(',', start); 

    string result = start <= 0 ? null 
    : stop >= 0 ? source.Substring(start, stop - start).Trim() 
    : source.Substring(start).Trim(); 
+1

可能想澄清“CM”字段保证在字符串的开头。 – Abion47

+0

@ Abion47:我真的很想在答案中提到这个关键条件。谢谢!好的赶上! –

0

我的解决方案,它的工作原理:)

public static string GetCertificateName(string certSubject) 
{ 
    //"CN=bbb1, OU=b, O=b, L=b, S=b, C=US" 
    System.Text.RegularExpressions.Regex regex; 
    try 
    { 
     regex = new System.Text.RegularExpressions.Regex(@"CN\s*=\s*(?<name>\*?\.?\w*\.?\w+)"); 
     var match = regex.Match(certSubject); 
     return match.Groups["name"].Value; 
    } 
    catch(Exception ex) 
    { 
     Console.WriteLine(ex.Message); 
    } 

    return "Can't parse"; 
} 
+0

你可以像'“CN = *。host。*,”'?在这种情况下,匹配是'“* .host”' –

+0

德米特里,感谢您的回复,在我的情况下,我可以假设输入不是* .host。*,但我真的没有考虑它。 –

0

不使用正则表达式的其他答案:

public static string Parsing(string certSubject) 
    { 
     string[] strArr = certSubject.Split(new char[] { '=', ' ', '\t', '\n',',' }); 
    string subject = ""; 

    for (int i = 0; i < strArr.Length; i++) 
    { 
     if (strArr[i].Equals("cn", StringComparison.InvariantCultureIgnoreCase)) 
     { 
      if ((i + 1) < strArr.Length) 
      { 

       subject = strArr[i + 1]; 
       break; 
      } 
     } 
    } 

    if (subject.Length > 0) 
    { 
     return subject; 
    } 
    return "Can't parse"; 
} 
+0

'string [] strArr = certSubject.Split(new char [] {'=','','\ t','\ n',',',StringSplitOptions.RemoveEmptyEntries);''将过滤掉所有空块 –

+0

你可以像这样输入:“CN = file:\\ c:\ Program Files \ MyFile.txt”;它是*空间*(在'Program'和'Files'之间)损坏风扇 –

+0

你是对的,我会坚持正则表达式的解决方案。 –