2017-01-15 98 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

你是對的,我會堅持正則表達式的解決方案。 –