【问题标题】:regex multiple replaces in one go using c++ regex使用 c++ 正则表达式一次进行正则表达式多个替换
【发布时间】:2013-05-29 10:48:11
【问题描述】:

我想用 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”之后的所有“@”

有人能给我一些启示吗?似乎没有两个人可以在旅途中完成所有这些工作。

【问题讨论】:

  • 我不明白用什么替换什么。
  • 他基本上是想删除@。我看不出这个任务的难度……
  • 没有。请仔细看看。想在 FROM 之前去掉 TableOne.@ColumnTwo,在 WHERE 之后去掉 @
  • @hongpei 好的,下次您应该真正指定并展示您尝试过的内容。 Here's 你是怎么做到的。

标签: c++ regex


【解决方案1】:

说明

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

示例

添加这是 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'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-24
    • 2013-02-16
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    相关资源
    最近更新 更多