Home > database >  If string contains one from the list with regex check
If string contains one from the list with regex check

Time:08-08

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. 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. And in my list i don't have 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

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] 

CodePudding user response:

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?"

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.

Question two is "Are any of the ouValues values exactly two uppercase letters?"

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

CodePudding user response:

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();
  • Related