c++ - 使用 C++ 正则表达式一次性替换多个正则表达式

标签 c++ regex

我想用C++正则表达式类来转

SELECT TableOne.ColumnOne, TableOne.@ColumnTwo, TableOne.ColumnThree FROM TableOne WHERE TableOne.@ColumnTwo ='abc'

进入

SELECT TableOne.ColumnOne, TableOne.ColumnThree FROM TableOne WHERE TableOne.ColumnTwo ='abc'

基本上,想做以下事情

(1)删除“FROM”之前的“TableOne.@ColumnTwo”之类的内容

(2)去掉“FROM”后面的“@”

有人可以给我一些启发吗?似乎没有直接的两个可以同时完成所有这些工作。

最佳答案

描述

正则表达式:^(.*?)TableOne[.]@ColumnTwo,\s+(.*?)[@](.*?)$ 替换为:\1\2\3

enter image description here

例子

添加此 C# 示例以显示正则表达式的工作原理。

using System;
using System.Text.RegularExpressions;
namespace myapp
{
  class Class1
    {
      static void Main(string[] args)
        {
          String sourcestring = "SELECT TableOne.ColumnOne, TableOne.@ColumnTwo, TableOne.ColumnThree FROM TableOne WHERE TableOne.@ColumnTwo ='abc'";
          String matchpattern = @"^(.*?)TableOne[.]@ColumnTwo,\s+(.*?)[@](.*?)$";
          String replacementpattern = @"\1\2\3";
          Console.WriteLine(Regex.Replace(sourcestring,matchpattern,replacementpattern,RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline));
        }
    }
}

$sourcestring after replacement:
SELECT TableOne.ColumnOne, TableOne.ColumnThree FROM TableOne WHERE TableOne.ColumnTwo ='abc'

关于c++ - 使用 C++ 正则表达式一次性替换多个正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16894134/

相关文章:

javascript - 将蛇形字符串转换为标题大小写

c++ - 将字符值输入到 int 数组中?

regex - 有没有一种有效的方法可以从 Postgres 中的列值中抓取子字符串?

javascript - 将字符串 ">number<"转换为 "><a href="tel :number">number</a><"

c++ - 使用 boost::shared_ptr 引用迭代器而不复制数据?

javascript - 将 html 标签放在字符串中的匹配项周围

java - 有人可以帮我澄清我对这个正则表达式的理解吗?

c++ - 在 .qrc 中添加 pdf 作为资源

c++ - list.begin() 上的段错误

python - 在 Mac OS X (10.7) 上安装 graph-tool - 已经安装了 Boost,但不断出现此错误