CN=Cal Sith/O=SEL,CN=Scot Shaf/O=SEL
上面的字符串看起来像这样,我试图提取名称(Cal Sith; Scot Shaf),可能会有两个以上的逗号分隔。
距离我越来越近,但又错了:
((?:[A-ZA-Z=]+[^a-zA-Z'-]+){0,12})\/O=SEL
最终,我希望他们在
List<String> FullNames
答案 0 :(得分:1)
为了从以下字符串获取名称: CN = Cal Sith / O = SEL,CN = Scot Shaf / O = SEL
尝试以下代码段:
string sPattern = @"(?:[a-z=]+)=(?<Name>[a-z\s]+)\/O=SEL";
Regex oRegex = new Regex(sPattern,RegexOptions.IgnoreCase);
string sInput = "CN=Cal Sith/O=SEL,CN=Scot Shaf/O=SEL";
List<string> FullNames = new List<string>();
foreach (Match oData in oRegex.Matches(sInput))
{
FullNames.Add(Convert.ToString(oData.Groups["Name"]));
}
答案 1 :(得分:0)
您可以使用正数lookbehind ?<=\bCN=)
断言前面是CN=
,并用负数character class [^/]+
来匹配不正斜杠或多次:
(?<=\bCN=)[^/]+
string pattern = @"(?<=\bCN=)[^/]+";
string input = @"CN=Cal Sith/O=SEL,CN=Scot Shaf/O=SEL";
List<String> FullNames = Regex.Matches(input, pattern)
.Cast<Match>()
.Select(match => match.Value)
.ToList();