mysql的正則替換方式

dimen007發表於2010-07-21

在為外來版本翻譯的時候,遇到這樣的問題。要對去除括號內所有字元,替換掉,但是mysql無法簡單的實現;找了下c++也不行,只能用更簡單的c#了,正則替換好用,對於正則替換還真不是很懂,linux還可以。c#就只能拿來主義了。有空研究研究下.

cmysql.cs

    using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication6
{
    class cmysql
    {
        public string c1;
        public int c2;
        public string c3;
        public int c4;
        public string c5;
        public string c6;
    }
}

main.cs

using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using System.Text.RegularExpressions;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {

            string sqlstr = "SELECT * FROM generalchestlistinfo";
            MySqlConnection DBConn = new MySqlConnection("server=127.0.0.1;User Id=test0;password=test;Database=koa");
            DBConn.Open();
            MySqlCommand DBComm = new MySqlCommand(sqlstr, DBConn);
            //MySqlDataReader DBReader = DBComm.ExecuteReader(); //DBComm.ExecuteReaderEx();
            MySqlDataAdapter DTAdapter = new MySqlDataAdapter(sqlstr, DBConn);

            DataSet myDataSet = new DataSet();
            DTAdapter.Fill(myDataSet, "tname");
           
            List lc = new List();
            Regex r = new Regex(@"\([^\)]*\)");
            //Regex r = new Regex(@"\(*\)");
            //Console.WriteLine(r.Replace("1(abc)2(2d4)3(12)" ,""));

            for (int i = 0; i < myDataSet.Tables[0].Rows.Count; i++)
            {
                cmysql cmy=new cmysql();
                cmy.c1 = myDataSet.Tables[0].Rows[i]["generalname"].ToString();
                //cmy.c3 = myDataSet.Tables[0].Rows[i]["Name"].ToString();
                cmy.c2 = Convert.ToInt32(myDataSet.Tables[0].Rows[i]["id"].ToString());
                //cmy.c4 = Convert.ToInt32(myDataSet.Tables[0].Rows[i]["level"].ToString());
                //cmy.c1=cmy.c1.Replace("地","天");
                lc.Add(cmy) ;
               
            }
            for(int i=0;i            {
                cmysql aa = lc[i];
                MySqlCommand mcommand = new MySqlCommand("UPDATE generalchestlistinfo SET `generalname` = '" + r.Replace(aa.c1, "") + "' WHERE id = " + aa.c2 , DBConn);
                mcommand.ExecuteNonQuery();
            }
            DBConn.Close();
            Console.WriteLine("finished");
            Console.ReadLine();
        }
     
       
    }
}

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12309491/viewspace-668653/,如需轉載,請註明出處,否則將追究法律責任。

相關文章