[.net] String comparison using wildcards.

Started by
5 comments, last by Arild Fines 17 years, 11 months ago
Is it possible to sarch substrings of a string using wildcards, for example if i wanted to find out if a string contained "santa*. Rides" ?
Advertisement
This should help ya...

http://www.codeproject.com/string/wildcmp.asp?df=100&forumid=2130&exp=0&select=957461

theTroll
The quicker but also much more powerful way is to use RegEx - it's built in to the .NET framework and should have everything you need.
Try these articles, but feel free to ask for help with explanations, etc.
http://www.radsoftware.com.au/articles/regexsyntaxadvanced.aspx
http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx

Hope it helps. :)
Look up the Regex class, and regular expression. If you're familiar with Perl's regex syntax, you'll pick it up quick.
[sub]My spoon is too big.[/sub]
Answer to post #1:
Yeah i found that earlier but didn't like it so i wrote my own. This is for searching inside my own little database that i'm writing for a program. Might not be the most optimised but it's simple.

public File[] Search(string searchPattern, bool ignoreCase, DatabaseSearchOptions searchOptions)        {            List<File> findList = new List<File>();            // We'll split up the searchPattern using the wildcards.            string[] wildcards = { "*", "?" };            // Check the logic.            if (ignoreCase)                searchPattern = searchPattern.ToLower();            // Split up the search pattern using the wildcards.            string[] comparers = searchPattern.Split(wildcards, StringSplitOptions.RemoveEmptyEntries);            // We need it for the search logic.            bool    found;            string  checkAgainst;            foreach (File file in fileList)            {                found = true;                // Having a switch inside a loop isn't something that's recommended performance wise.                switch (searchOptions)                {                    case DatabaseSearchOptions.NameOnly:                        checkAgainst = file.Name;                        break;                    case DatabaseSearchOptions.PathOnly:                        checkAgainst = file.Path;                        break;                    case DatabaseSearchOptions.NameAndPath:                        checkAgainst = file.Path + "\\" + file.Name;                        break;                    default:                        checkAgainst = file.Name;                        break;                }                if (ignoreCase)                    checkAgainst = checkAgainst.ToLower();                // Compare the string against the comparers.                foreach (string c in comparers)                {                    // No occurence found.                    if (checkAgainst.IndexOf(c) == -1)                    {                        found = false;                        break;                    }                }                // We found a match.                if (found)                    findList.Add(file);            }            return findList.ToArray();        }


Post(s) 1+:

Thanks, i'll look into it :)
Regexes are the way to go. Any other solution will be a poor, partial regex implementation. Don't just look into it, use it.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
One day Unixbeard had a problem. "I know, " he said, "I'll use regular expressions." Then Unixbeard had two problems.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

This topic is closed to new replies.

Advertisement