使用正则表达式、组捕获和反向引用查找和替换 [英] Find and replace using regular expression, group capturing, and back referencing

查看:40
本文介绍了使用正则表达式、组捕获和反向引用查找和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 SQL Server 2008 R2 Management Studio 中执行查找和替换操作并使用组捕获,以便我可以回参考替换中的组.我从 this 了解到 SSMS 使用 Visual Studio 2005 正则表达式引擎.这是我所拥有的示例:

I'm trying to perform a find and replace operation in SQL Server 2008 R2 Management Studio and employ a group capture so that I can back reference the groups in the replacement. I understand from this that SSMS uses the Visual Studio 2005 regex engine. Here is an example of what I have:

SELECT First FROM Table1
SELECT Second FROM Table2
SELECT Third FROM Table3

这是我的查找"表达式:

Here is my "Find" expression:

SELECT (.+) FROM (.+)

这是我的替换"表达式:

Here is my "Replace" expression:

\1 \2

然而,运行替换操作不会返回任何捕获的组,即每一行只是"(<---只是两个反向引用之间的空间).

However, running the replace operation results in no captured groups being returned, i.e. each line is just " " (<---just the space between the two back-references).

我做错了什么?

推荐答案

SQL Server Management Studio 引擎使用 {} 来标记"表达式:

SQL Server Management Studio engine uses { and } to "tag" expressions:

SELECT {.+} FROM {.+}

当您将其与 \1 \2 替换表达式结合使用时,您会得到预期的结果:

When you combine this with the \1 \2 replacement expression, you get the expected result:

First Table1
Second Table2
Third Table3

VS 正则表达式参考页解释细节.

这篇关于使用正则表达式、组捕获和反向引用查找和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆