tags:

views:

445

answers:

3

Hi there,

I am trying to parse thru a csv string, put the results into a IList collection and then trying to find a way to do a wildcard 'contains' based on what was passed in. Right now I have the following:

    public static IList<string> DBExclusionList
    {
        get 
        {
            Regex splitRx = new Regex(@",\s*", RegexOptions.Compiled);
            String list = (string)_asr.GetValue("DBExclusionList",typeof(string)); 
            string[] fields = splitRx.Split(list); 
            return fields; 
        }
    }
    if (DBExclusionList.Contains(dbx.Name.ToString())==false)
    {...}

So if the string I am parsing (key value from .config file) contains: key="DBExclusionList" value="ReportServer,ReportServerTempDB,SQLSentry20,_TEST"

The DBExclusionList.Contains() works very well for exact matches on the first 3 items in the list, but I want to be able to ALSO have it for any partial match of the fourth item '_TEST'

is there any way to do it? I could certainly hardcode it to always exclude whatever but I'd rather not.

thanks.

+2  A: 

Since you're using .NET 3.5, you could use the Where() LINQ extension method:

DBExclusionList.Where(item => item.Contains(dbx.Name.ToString()))
jrummell
A: 

Don't use regex to split. Use string.Split() and delimit your _asr.GetValue("DBExclusionList) with something like semi-colons.

Each item in _asr.GetValue("DBExclusionList) should be a regex pattern, so you check against each one in turn.

Neil Barnwell
+2  A: 

Using Linq :

if (DBExclusionList.Any(s => s.Contains(dbx.Name.ToString())))
Thomas Levesque
I went with this one since the .Any returns a bool. thanks :)
SomeMiscGuy