my coding to ignorecase isn't working but the else statement does. I have system.using regular expressions allready. it runs without errors but it dosn't ignore capitals.

private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
               Regex word = new Regex
               (wordtextBox.Text, RegexOptions.IgnoreCase);// dosn't work
            }
            else
            {
                Regex word = new Regex(wordtextBox.Text); // this here works
            }

thanx for reading anyway.

Recommended Answers

All 4 Replies

Have you tried the .Net function ToUpper or ToLower? Here is the MSDN information on the function.

ToUpper Documentation

You aren't very clear, what "doesn't work" since you get no errors? You do realize that the Regex you are creating only has scope inside the if, once you leave that there is no Regex?

toupper & tolower would work but having it ignore cases is better. i have a textbox the user can put in wot they want to find. iv put orginal code in to my main loop.

foreach (string file in files)
                    {
                        if (file.Contains(".txt"))
                        {
                            inputstream = File.OpenText(file);
                            words = inputstream.ReadLine().Split();

heres the code from up top  //     if (checkBox1.Checked == true)
                            {
                                Regex word = new Regex
                                (wordtextBox.Text, RegexOptions.IgnoreCase);
                            }
                            else
                            {
                                Regex word = new Regex(wordtextBox.Text);
                            }                                              ////////

                            foreach (string word in words)
                            {

                                if (word == wordtextBox.Text)
                                 resultslistBox.Items.Add(word + " - " + file); 
                            }
                            inputstream.Close();

still does the same job. maby i still dont have it in the right place. so if user puts in capitals the search dosnt execute. otherwise its fine

if (file.Contains(".txt")) {
    inputstream = File.OpenText(file);
    words = inputstream.ReadLine().Split();
 
    if (checkBox1.Checked == true) {
        Regex word = new Regex(wordtextBox.Text, RegexOptions.IgnoreCase);
    } else {
        Regex word = new Regex(wordtextBox.Text);
    }

    foreach (string word in words) {
        if (word == wordtextBox.Text) {
            resultslistBox.Items.Add(word + " - " + file);
        }
    }
    inputstream.Close();
}

A few things wrong in this code. As I said before, the scope (or the lifetime) of the Regex statements you made above is only in their if block sections (For example, the Regex declared in line 6 ceases to exist once you evaluate line 7. You should read about Scope.

Even if this didn't happen, your line 11 defines another variable with the name 'word' which would 'mask' visibility to the other variables named 'word'.

This should do what you are looking for:

if (file.Contains(".txt")) {
    inputstream = File.OpenText(file);
    words = inputstream.ReadLine().Split();
       
    foreach (string word in words) {
        if (String.Compare(word, wordtextBox.Text, checkBox1.Checked) == 0) {
            resultslistBox.Items.Add(word + " - " + file);
        }
    }
    inputstream.Close();
}
Be a part of the DaniWeb community

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