List Error on Load Balanced Server

Ice

Member
Joined
Jan 25, 2013
Messages
14
Programming Experience
Beginner
Hi

I'm running text through an IllegalCharacter Class method that
runs through a list. I've put it live and now I keep getting this error
System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Collections.Generic.List`1.Enumerator.MoveNextRare() at IllegalCharacters.Filter(String sText)

I've seen people talk about locking the thread but I'm not sure how to implement that or even if that
would work.

The code is called as follows on a different page

C#:
IllegalCharacters.IllegalCharRegPatterns();
Message = IllegalCharacters.Filter(sMessage);

The Code for IllegalCharacter Class is
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for IllegalCharacters
/// </summary>
public class IllegalCharacters
{
    public static List<RegPatternParameters> lstIllegalParameters = new List<RegPatternParameters>();
    
    public static void IllegalCharRegPatterns()
    {
        //List Layout = RegExp, hide?, send owner email?

        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\b[A-Z0-9._%+-]+@+[A-Z0-9.-]+\\.+[A-Z]{2,4}\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\b[A-Z0-9._%+-]+[\\( -]+AT+[\\) -]+[A-Z0-9.-]+\\.+[A-Z]{2,4}\\b)", bHide = false, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\b[A-Z0-9._%+-]+[\\( -]+AT+[\\) -]+[A-Z0-9.-]+[\\( -]+DOT+[\\) -]+[A-Z]{2,4}\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\b[A-Z0-9._%+-]+[\\( -]+DOT+[\\) -]\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = @"(\\bhttp\\:\\/\\/[A-Z0-9\/_.-]+\\.[A-Z]{2,4}\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = @"(\\bwww\\.[A-Z0-9\/_.-]+\\.[A-Z]{2,4}\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\b[A-Z0-9.-]+\\.com\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\b[A-Z0-9.-]+\\.co\\.za\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\b[A-Z0-9.-]+\\.travel\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\b[A-Z0-9.-]+\\.net\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\b[A-Z0-9.-]+\\.org\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\b([ \\-\\,\\.\\(\\)\\+]?[ \\-\\,\\.\\(\\)\\+]?[ \\-\\,\\.\\(\\)\\+]?\\d{1,}){8,}\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\b( ||)((zero)|(one)|(two)|(three)|(four)|(five)|(six)|(seven)|(eight)|(nine))( ||)(\\.|\\,|-||)( ||)((zero)|(one)|(two)|(three)|(four)|(five)|(six)|(seven)|(eight)|(nine))( ||)(\\.|\\,|-||)( ||)((zero)|(one)|(two)|(three)|(four)|(five)|(six)|(seven)|(eight)|(nine))( ||)(\\.|\\,|-||)( ||)((zero)|(one)|(two)|(three)|(four)|(five)|(six)|(seven)|(eight)|(nine))( ||)(\\.|\\,|-||)( ||)((zero)|(one)|(two)|(three)|(four)|(five)|(six)|(seven)|(eight)|(nine))\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\b( ||)((nul)|(een)|(twee)|(drie)|(vier)|(vyf)|(ses)|(sewe)|(agt)|(nege))( ||)(\\.|\\,|-||)( ||)((nul)|(een)|(twee)|(drie)|(vier)|(vyf)|(ses)|(sewe)|(agt)|(nege))( ||)(\\.|\\,|-||)( ||)((nul)|(een)|(twee)|(drie)|(vier)|(vyf)|(ses)|(sewe)|(agt)|(nege))( ||)(\\.|\\,|-||)( ||)((nul)|(een)|(twee)|(drie)|(vier)|(vyf)|(ses)|(sewe)|(agt)|(nege))( ||)(\\.|\\,|-||)( ||)((nul)|(een)|(twee)|(drie)|(vier)|(vyf)|(ses)|(sewe)|(agt)|(nege))\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bbooksure\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bsearch\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bcontact\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bnumber\\b)", bHide = false, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bcell\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bmobile\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bphone\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bselfoonnommer\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bkontak\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bdirect email\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bdirectly\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bdirect\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bcommission\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bcomission\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bcommision\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bcomision\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bemail\\b)", bHide = false, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bwebmail\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bskype\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bdirectory enquiries\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bfacebook\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\btwitter\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bgesigboek\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bgoogle\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bgmail\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\byahoo\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bcom\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bco\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bza\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\buk\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bau\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bwww\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bfull stop\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bsafarinow\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bsafari now\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bcapestay\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bcape stay\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bsavenues\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bwheretostay\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\broomsforafrica\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\brooms for africa\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bresafrica\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\btrip advisor\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bfacebook\\b)", bHide = true, bSendMail = true });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\brennies travel\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\brennies\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\binternet\\b)", bHide = false, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bweb site\\b)", bHide = false, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bwebsite\\b)", bHide = false, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bfees\\b)", bHide = false, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bamadea travel\\b)", bHide = false, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bbig five promotions\\b)", bHide = false, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bselfoon\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bepos\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bdirek\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bnavrae\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bkommissie\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bwebwerf\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bweb werf\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bfone\\b)", bHide = true, bSendMail = false });
        lstIllegalParameters.Add(new RegPatternParameters { sRegPattern = "(\\bnomber\\b)", bHide = true, bSendMail = false });
    }

    public class RegPatternParameters
    {
        public string sRegPattern { get; set; }
        public bool bHide { get; set; }
        public bool bSendMail { get; set; }
    }

    public static string ConvertSTDWords(string sText)
    {
        //var enquiry = new RegExp("\\b" + iResID.toString(10) + "\\b", "g");
        //TxT = TxT.replace(enquiry, "<ENQUIRY>");

        sText = Regex.Replace(sText, "\\b" + StdVariables.dtNow.Year.ToString() + "\\b", "<CYEAR>");
        sText = Regex.Replace(sText, "\\b" + (StdVariables.dtNow.Year - 1).ToString() + "\\b", "<PYEAR>");
        sText = Regex.Replace(sText, "\\b" + (StdVariables.dtNow.Year + 1).ToString() + "\\b", "<NYEAR>");
        sText = sText.Replace(@"www\.testing-out\.co\.za", "<WWWtestingOUT>");
        sText = sText.Replace(@"testing-out\.co\.za", "<testingOUT>");
        sText = sText.Replace(@"testingout\.co\.za", "<testingOUT>");
        sText = sText.Replace(@"testing-out\.com", "<testingOUT>");
        sText = sText.Replace(@"testingout\.com", "<testingOUT>");
        sText = sText.Replace(@"www\.testingOther-out\.co\.za", "<WWWtestingOtherOUT>");
        sText = sText.Replace(@"testingOther-out\.co\.za", "<testingOtherOUT>");
        sText = sText.Replace(@"testingOtherout\.co\.za", "<testingOtherOUT>");

        return sText;
    }

    public static string Filter(string sText)
    {
        sText = ConvertSTDWords(sText);

        foreach (RegPatternParameters regPattern in lstIllegalParameters)
        {
            Regex regCompiledPattern = new Regex(regPattern.sRegPattern);
            if (regPattern.bHide)
            {
                sText = regCompiledPattern.Replace(sText, " ");
            }
        }

        sText = ReturnSTDWords(sText);

        return sText;
    }

    public static string ReturnSTDWords(string sText)
    {
        //var enquiry = new RegExp(iResID.toString(10), "g");
        //sText = sText.Replace("<ENQUIRY>",iResID.toString(10));

        sText = sText.Replace("<CYEAR>", StdVariables.dtNow.Year.ToString());
        sText = sText.Replace("<PYEAR>", (StdVariables.dtNow.Year - 1).ToString());
        sText = sText.Replace("<NYEAR>", (StdVariables.dtNow.Year + 1).ToString());
        sText = sText.Replace("<WWWSLEEPIT>", @"www.testing-out.co.za");
        sText = sText.Replace("<testingOUT>", @"testing-out.co.za");
        sText = sText.Replace("<WWWtestingOtherOUT>", @"www.testingOther-out.co.za");
        sText = sText.Replace("<testingOtherOUT>", @"testingOther-out.co.za");

        return sText;
    }

}
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,183
Location
Norway
Programming Experience
10+
Top Bottom