使用正则表达式、组捕获和反向引用查找和替换

编程入门 行业动态 更新时间:2024-10-11 17:26:43
本文介绍了使用正则表达式、组捕获和反向引用查找和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在 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 正则表达式参考页解释细节.

更多推荐

使用正则表达式、组捕获和反向引用查找和替换

本文发布于:2023-11-06 03:26:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562606.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!