Hi.
I'm trying to create a 'search engine' like program. With a bit of searching i found this website which was relevant to what i needed to do.http://www.dreamincode.net/code/snippet1278.htm

if (Regex.IsMatch(allRead, regMatch))//If the match is found in allRead
            {
                Console.WriteLine("found\n");
                Console.WriteLine(regMatch);
                Console.Read();

            }

I thought "console.writeline(regmatch)" would display the related searches however i realised regmatch is a string which contains what the user initially searched for. is there a way to display all related items?

if (Regex.IsMatch(allRead, regMatch)) {
    MatchCollection matches = Regex.Matches(allRead, regMatch);
    foreach (Match m in matches) {
        Console.WriteLine(m.Value);
    }
}
if (Regex.IsMatch(allRead, regMatch)) {
    MatchCollection matches = Regex.Matches(allRead, regMatch);
    foreach (Match m in matches) {
        Console.WriteLine(m.Value);
    }
}

thanks for the reply, this seems to be what i'm looking for, however it displays what the user has typed x amount of times something similar has been found

For example, the file might contain the following text
florida
floor
florentina
flored

if the user types in "flor", then the program should display the closest matches, so in this case "florida, florentina and flored"

Edited 5 Years Ago by benjie987: n/a

Then you need a better Regex expression. Something like Regex reg = new Regex("^.*" + yourText + ".*$") The first section says "match from the beginning of the line zero or more characters".
Last section is "match zero or more characters and the end of the line".

You need to be careful what 'yourText' is. If it contains characters used by regular expressions you will probably get odd results.

i'm new to c# and i have been told certain characters have a meaning....do the symbols in "("^.*" + yourText + ".*$")" have any meaning?

There are two parts to the statement. The first is the call to the constructor new Regex() and the parameter passed to the constructor "^.*" + yourText + ".*$" .

The parameter itself consists of three part "^.*" , yourText and ".*$" .

The first part is a regular expression meaning 'start of line and any number of characters'. The '^' symbol means match the start of the line. The '.' character means match anything (number, symbol, control character, etc.). The '*' is a modifier to the previous, meaning 'zero or more'.

The second part is the text you are trying to find.

The third part is like the first, with the '.*' having the same meaning as before. The '$' on the end means 'match the end of the line'.

So the whole thing is 'find a line that contains any number of characters, the string I want to find followed by any number of characters'.

This article has been dead for over six months. Start a new discussion instead.