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


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?

Recommended Answers

All 8 Replies

And what exactly would you like to search? Tell me more.

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

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

Because that is what it matched. What do you want it to display?

For example, the file might contain the following text

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

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'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.