简体   繁体   English

如果字符串包含列表中的一个,使用正则表达式检查

[英]If string contains one from the list with regex check

I want to check if OU from my list is contained in this string, and i want it to check if the OU= has two symbols.我想检查我的列表中的 OU 是否包含在此字符串中,并且我希望它检查 OU= 是否有两个符号。 For example in my list i have EE country code, but in this example string i have OU=EER, but it's not country code.例如,在我的列表中,我有 EE 国家/地区代码,但在此示例字符串中,我有 OU=EER,但它不是国家/地区代码。 And in my list i don't have OU=NL.在我的列表中,我没有 OU=NL。 I understand that i need to check if the string which i found has two chars, but i don't know how.我知道我需要检查我找到的字符串是否有两个字符,但我不知道如何。

String:细绳:

CN=nlpgebl,OU=Users,OU=C3176172,OU=EER,OU=NL CN=nlpgebl,OU=用户,OU=C3176172,OU=EER,OU=NL

Code:代码:

var countries = new List<string> { "AT", "HR", "BG", "CZ", "EE", "GR", "HU", "LT", "LV", "MK", "PL", "RO", "RS", "SI", "SK", "TR" };
if (countries.Any(nodes[5].InnerText.Contains)) // Regex that i think need here: OU=[a-zA-Z]+

Your input string is a comma separated key=value pair.您的输入字符串是逗号分隔的键=值对。 Use that to your advantage, and there's no need for regex.使用它对您有利,并且不需要正则表达式。

Split by comma, split each element by equals, and filter the list by the correct prefix.用逗号分割,用等号分割每个元素,并用正确的前缀过滤列表。 Then you can get the two answers your interested in.然后你可以得到你感兴趣的两个答案。

var input = "CN=nlpgebl,OU=Users,OU=C3176172,OU=EER,OU=NL";
IEnumerable<string> ouValues = input
    .Split(',')
    .Select(x => x.Split('=', 2)) // "2" just in case the value has an equals in it
    .Where(x => x[0] == "OU")
    .Select(x => x[1]);

var targetCountries = new List<string> { "AT", "HR", "BG", "CZ", "EE", "GR", "HU", "LT", "LV", "MK", "PL", "RO", "RS", "SI", "SK", "TR" };

Question one is "Do any of ouValues entries equal a value in targetCountries ?"问题一是“是否有任何ouValues条目等于targetCountries中的值?”

var inputContainsCountry = ouValues.Intersect(targetCountries).Any();

If later on you want to know which target country is in the input string, you'll need to get rid of Any to see the list of matching values, then handle the collection length accordingly.如果稍后您想知道输入字符串的目标国家/地区,则需要去掉Any以查看匹配值列表,然后相应地处理集合长度。

Question two is "Are any of the ouValues values exactly two uppercase letters?"问题二是“是否有任何ouValues值正好是两个大写字母?”

var containsCountryCode = ouValues.Any(val => val.Length == 2 && char.IsLetter(val[0]) && char.IsLetter(val[1]));
// or
var containsCountryCode = ouValues.Any(val => Regex.IsMatch(val, @"^[A-Z]{2}$"));

The regex here simply checks for two uppercase letters, and the ^ and $ just ensure it's the only content in the string.这里的正则表达式只是检查两个大写字母,而^$只是确保它是字符串中的唯一内容。

Printing the values you get打印你得到的值

Console.WriteLine("Contains a target country: " + inputContainsCountry);
Console.WriteLine("Contains any country code: " + containsCountryCode);

Contains a target country: False
Contains any country code: True

Try following:尝试以下操作:

            string input = "CN=nlpgebl,OU=Users,OU=C3176172,OU=EER,OU=NL";
            string pattern = "OU=(?'value'[A-Za-z0-9]+)";
            MatchCollection matches = Regex.Matches(input, pattern);
            int count = matches.Count;
            foreach (Match match in matches)
            {
                string value = match.Groups["value"].Value;
                Console.WriteLine("Value =  {0}, count = {1}",value, value.Length);

            }
            Console.ReadLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM