locked
Trouble with Regex.Replace ... need a lil help... RRS feed

  • Question

  • im trying to create a bbcode substitute using Regex...
    im storing my Expression code in a sql database and retrieving them using a datareader...
    then storing them using a for loop in (what i think is ) an array of Regex..
    then im i run another for loof that try to convert the data but it fails to convert....

    please help or gimme an alternative if it cant be done..

    my code is...

    Code Block

    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;
    using System.Data.SqlClient;

    public partial class replace : System.Web.UI.Page
    {
        ArrayList code = new ArrayList();
        ArrayList smilie_image = new ArrayList();
        ArrayList smilie_alt = new ArrayList();

        public int a;

        protected void Page_Load(object sender, EventArgs e)
        {
                SqlConnection con_sqlconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                SqlCommand con_sqlCommand;
                SqlCommand Smilies;
                SqlDataReader DBReader;
                Smilies = new SqlCommand("Select count(*) from sr_smilies", con_sqlconnection);
                con_sqlCommand = new SqlCommand("Select * from sr_smilies", con_sqlconnection);

                con_sqlconnection.Open();
                a = (int)Smilies.ExecuteScalar();
                con_sqlconnection.Close();

                con_sqlconnection.Open();
                DBReader = con_sqlCommand.ExecuteReader();

                //DBReader.Read();
                //for (int i = 0; i < a; i++)
                while (DBReader.Read())
                {
                    code.Add(DBReader.GetString(DBReader.GetOrdinal("code")));
                    smilie_image.Add(DBReader.GetString(DBReader.GetOrdinal("smile_url")));
                    smilie_alt.Add(DBReader.GetString(DBReader.GetOrdinal("emotion")));
                }

                con_sqlconnection.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Regex[] smilies = new Regex[ a];
            for (int i = 0; i < a; i++)
            {
                smilies[ i] = new Regex((string)code[ i]);
            }

            string text = TextBox1.Text;

            for (int i = 0; i < a; i++)
            {
                text = smilies[i].Replace(text, "<img src='images/" + smilie_image[ i].ToString() + ".gif' alt='" + smilie_alt[ i].ToString() + "' />");
                }
            /*
            Regex test = new Regex("\\[:\\)\\]");
            string text = TextBox1.Text;
            Label1.Text = test.Replace(text, "<img src='images/" + smilie_image[ i].ToString() + ".gif' alt='" + smilie_alt[ i].ToString() + "' />");
            */
            Label1.Text = text1;
        }
    }


    help will be greatly appreciated...
    MSDN Link >> http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2222401&SiteID=1

    Thursday, October 4, 2007 7:09 AM

All replies

  • I am a bit confused by your code. Does the "code" array store the Regex patterns? It doesn't make any sense.

     

    Also, your pattern is wrong: "Regex test = new Regex(\\[:\\)\\]);" <-- you must specify the start "^" and end "$" of the pattern and even if you do, its highly generic and its likely to match abstract strings. I still don't see what exactly you are trying to match.

     

    Can you provide some examples?

     

    Judging by the looks of what you are trying to do, you don't need regex. Its a common misconception to believe regex is fast. In fact, regex is much slower than normal text parsing and you seem to be just wanting to do some simple string operations. You are much better of using normal string parsing/replacement. It will be a lot faster and cleaner. Especially since you seem to be using this in a bulleting board kinda environment, regex is comparatively more CPU intensive. 100s of thousands of requests at the same time could probably kill your server.

     

    P.S ArrayList is seriously outdated. Might want to think about using generics instead? System.Collections.Generic Smile

    Sunday, October 21, 2007 5:26 AM