Question

I am trying to scrub a list of items using another list and it's working fine except for its not ignoring case. When I try to add ordinal or regex casing checks, I get a syntax error. Can someone tell me what I am doing wrong? Here is my code:

List<string> removeChars = new List<string>(textBox_ScrubList.Text.Split(','));
            for (int i = 0; i < sortBox1.Count; i++)
                foreach (string repl in removeChars)
                    sortBox1[i] = sortBox1[i].Replace(repl, "", RegexOptions.IgnoreCase);

And here is the syntax error I am getting:

Regex Casing Syntax Error

Was it helpful?

Solution 2

So I figured it out. The last line:

 sortBox1[i] = sortBox1[i].Replace(repl, "", RegexOptions.IgnoreCase);

had to be changed to:

 sortBox1[i] = Regex.Replace(sortBox1[i], repl, "", RegexOptions.IgnoreCase);

OTHER TIPS

Assuming sortBox1 is a List<string> or similar, the problem is that String.Replace doesn't have any overload which takes a RegexOptions.

You can use Regex.Replace, but in that case you should probably be able to construct a single regular expression to remove all the characters in one go. If you do want to remove them one at a time, you may want to use Regex.Escape to avoid regular expression patterns from being a problem. (For example, if it tried to replace "." with "", you'd end up getting rid of everything.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top