Link to home
Start Free TrialLog in
Avatar of Sootah
SootahFlag for United States of America

asked on

RegEx.Replace() not working like I want it to

I want to retain the case of the line that I'm checking for obscenities, but I can't get the regex.replace to do it's job. Right now I have to convert the line to lowercase and then remove the offending word that way. I want to leave the line as is but still remove the profanity.

How do I get RegEx.Replace() to replace what I want it to and leave the rest of the line alone case wise?

I don't want to have to test for
Crap
crap
CRAP
cRaP
etc

individually.


else //If the option to remove the whole line is unchecked then remove only the offending word
            {
                for (x = 0; x <= sArray.GetUpperBound(0); x++)
                {
                    for (y = 0; y <= sBadArray.GetUpperBound(0); y++)
                    {
                        if (sBadArray[y].ToString().Trim() != "" && sArray[x].ToString().ToLower().Contains(sBadArray[y].ToString().ToLower()))
                        {
                            sArray[x] = sArray[x].ToLower();
                            sArray[x] = sArray[x].ToString().Replace(sBadArray[y].ToString().ToLower().Trim(), "");
                            //Regex.Replace(sArray[x].ToString().ToLower(), sBadArray[y].ToString().ToLower().Trim(), "");
                            numBadWords += 1;
                        }
                    }
                    if (sArray[x].ToString().Trim() != "") sBuff.Append(sArray[x].ToString().Trim() + "\r\n");
                }
            }
ASKER CERTIFIED SOLUTION
Avatar of jonorossi
jonorossi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sootah

ASKER

My bad, I forgot to mention that I tried the RegExOptions.IgnoreCase:

Regex.Replace(sArray[x], sBadArray[y].Trim(), "", RegexOptions.IgnoreCase);


I cannot think of ANY reason this doesn't work. It pops up the messagebox at the end: "1 obscenity removed!" and yet the RegEx.Replace statement has done NOTHING. Why?
Avatar of Sootah

ASKER

God. That's because I'm an idiot.

sArray[x] = Regex.Replace(sArray[x], sBadArray[y], "", RegexOptions.IgnoreCase);

I forgot the sArray[x] = part.

I'm going t go drown myself now. :)

G.I.G.O., eh?