惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Schneier on Security
Schneier on Security
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
A
About on SuperTechFans
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
V2EX - 技术
V2EX - 技术
N
Netflix TechBlog - Medium
F
Fortinet All Blogs
V
Visual Studio Blog
Martin Fowler
Martin Fowler
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
Scott Helme
Scott Helme
H
Heimdal Security Blog
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V
Vulnerabilities – Threatpost
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
Jina AI
Jina AI
S
Securelist
小众软件
小众软件
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
博客园 - 三生石上(FineUI控件)
量子位

博客园 - yiyisawa

岁月如梭 wcf client与webservice通信备注 将wcf 以webservice的方式调用 wcf client与webservice通信(-)只修改配置文件而改变服务端 WPF界面 -----类似于visual studio中浮动窗体风格的界面(以下地址提供源码下载) BackgroundWorker学习一 vs 工具技巧手册 silverlight发布注意事项 silverlight beta2初步研究结论 Visual Studio 2005 Team Foundation Server (TFS)单服务器安装记 从今天起 乱弹 盲山有感 路边见闻 月夜奔跑 无题 童年拾忆 梦想高歌 《勇敢抉择》摘录一
一个用Regex的完成sql语句中字段替换的demo
yiyisawa · 2008-01-02 · via 博客园 - yiyisawa
 

这两天在小demo。功能实现的是在一个template中(string Type,实际上是一个SQL select查询语句),用正则表达式查询出符合条件的字段,然后用另外一些值替换成实际可用的查询语句,然后显示出来。

因为开始需求自已弄的还不是很明白。所以琢磨了一阵,才理出思路。

大概实现步骤的思路是:

1、            因为template是在一个arrayinfo里的,所以先找到arrayinfo,然后再找到arrayinfo包含的template,并取出此值并赋给另一个string类型的变量(这样做只是看得清晰些);

                IEnumerator IEatorArrayinfoList = arrayinfo.GetEnumerator();

                 while (IEatorArrayinfoList.MoveNext())

                 {

                     RuleTypeInfo ruleInfo = (RuleTypeInfo)IEatorArrayinfoList.Current;

                     if (cboRuleType.SelectedValue.ToString() == ruleInfo.RuleTypeID.ToString())

                     {

                         strSelect = ruleInfo.RuleTypeTemplate;

                     }

      }

2、            使用正则表达式(Regex)。使用正则表达式的时候费了一些时间,因为之间学的都忘记得干干净净的了。只好又找了一篇文章《正则表达式30入门教程》,因为此文写得比较基础,而且通俗易懂,大概意思是理解了,但是实际操作起来还是有点困难。所以又在琢磨了一些时候,才将两个正则表达式写出来,并添加到ArrayList中,与所需求的是一样的。
 //获取需替代的字段

        public static ArrayList showMatches(string expression, RegexOptions option, string ms)

        {

            Regex regex = new Regex(expression, option);

            MatchCollection matches = regex.Matches(ms);

            ArrayList list = new ArrayList();

            foreach (Match m in matches)

            {

                list.Add(m.ToString());

            }

            return list;

}

   //获取用正则表达式取出的字段(一种带&.,一种只是纯粹的字段)

          listSign = showMatches(@""&"w*"d*".", RegexOptions.None, strSelect);

 listField = showMatches(@"(?<=&)"w*"d*[^&.](?=".)", RegexOptions.None, strSelect);

3、            将即将要替换上的值的名字与值添加到HashTable里(值的名字为key,值为value)。

//将paralist中的参数填充hashtable

                    this.gridParameters.Items.Refresh();

                    txtName.Focus();

                    IEnumerable IEnumberTableList = this.gridParameters.Items.SourceCollection;

                    IEnumerator IEatorList = IEnumberTableList.GetEnumerator();

                    while (IEatorList.MoveNext())

                    {  

                        ParamersGridInfo infoList = (ParamersGridInfo)IEatorList.Current;

                        htRulePara.Add(infoList.RuleTypeParaName, infoList.RuleTypeParaValue);

                    }

4、            然后循环将HashTable中的key与用正则表达式查出来的字段(ArrayList[i])对比,如果相同,就用string.Replace替换就可以了,然后就是一个新的SQL select查询语句了。

//获取替换后的sql select语句

                    foreach (DictionaryEntry de in htRulePara)

                    {

                        for (int i = 0; i < listField.Count; i++)

                        {

                            if (de.Key.ToString() == listField[i].ToString())

                            {

                                strSelect = strSelect.Replace(listSign[i].ToString(), de.Value.ToString());

                            }

                        }

                    }

                    MessageBox.Show(strSelect,Constant.SystemTitle, MessageBoxButton.OK,MessageBoxImage.Information);