正则表达式替换嵌套括号匹配内的字符,或仅替换匹配外的文本 [英] Regex replace character within match of nested parenthesis, or replace only within text outside of match

查看:28
本文介绍了正则表达式替换嵌套括号匹配内的字符,或仅替换匹配外的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 AutoHotkey 脚本,该脚本将从屏幕上选择的文本格式化 SQL 语句.我想转一个这样的语句:

I'm writing an AutoHotkey script that will format SQL statements from text selected on the screen. I want to turn a statement like this:

SELECT Name AS [Object Name], Switch([Type]=5,'Query',[Type]=-32768,'Form',[Type]=6,'Table') AS [Object Type], Switch([Type]=5,1,[Type]=-32768,2,[Type] In (1,4,6),6) AS [Object Type ID], Left(Name,4) as Prefix, LTrim(RTrim(Mid([Name],5,30))) as Suffix

进入这个:

SELECT Name AS [Object Name], 
    Switch([Type]=5,'Query',[Type]=-32768,'Form',[Type]=6,'Table') AS [Object Type], 
    Switch([Type]=5,1,[Type]=-32768,2,[Type] In (1,4,6),6) AS [Object Type ID], 
    Left(Name,4) as Prefix,
    LTrim(RTrim(Mid([Name],5,30))) as Suffix

我首先用逗号+回车+制表符替换逗号,但是当我遇到包含在括号内使用逗号的函数的SQL语句时,它产生了不希望有的结果.我的第一个解决方案是使用 AutoHotkey RegEx 命令排除括号内的逗号:

I started by replacing commas with comma+carriage return+tab but when I encountered SQL statements containing functions using commas within parenthesis it produced undesirable results. My first solution was to exclude commas within parenthesis, with this AutoHotkey RegEx command:

; Find commas not in parenthesis and suffix with <CR><Tab>
s := RegExReplace( s, ",(?![^()]*))", ",`r`n" . Tab )

问题是有时括号是嵌套的,而那个简单的正则表达式不起作用.

The problem is that sometimes parenthesis are nested, and that simple RegEx didn't work.

经过一番挖掘,我找到一个递归正则表达式,可以选择每组最外面的括号.

After some digging I found a recursive RegEx that would select the outer most parenthesis of each group.

((?:[^()]++|(?R))*)

现在的挑战是,

  1. 如何选择该组之外的所有内容并在其中查找/替换,
  2. 如何仅对该组内的文本应用搜索/替换?

正则表达式演示

SO 鼓励我们回答我们自己的问题.在写这篇文章的过程中,我找到了一个解决方案,我会把它贴在下面.随意分享您自己的解决方案.我想进一步了解正则表达式.

SO encourages us to answer our own question. In the process of writing this up I found a solution and I will post it below. Feel free to share solutions of your own. I would like to further my understanding of regular expressions.

推荐答案

我发现我可以在表达式中使用 or 来查找括号中的任何内容 OR 任何逗号.使用此方法,它不会选择括号组内的任何单个逗号.(感谢这篇帖子中的zx81.)

I discovered that I can use an or in my expression to find anything in parenthesis OR any comma. With this method it won't select any individual commas that are inside the parenthesis groups. (Thanks to zx81 in this post.)

 ,|((?:[^()]++|(?R))*)

通过该表达式,我可以使用替换 |$0|| 字符包围每个匹配组.然后很容易找到带有 |,| 的独立逗号并替换为我的回车模式,然后将所有剩余的 | 替换为空字符串.

With that expression I can use the substitution |$0| to surround each matching group with the | character. Then it is easy to find the stand-alone commas with |,| and replace with my carriage return pattern, then replace all remaining |'s with an empty string.

; AutoHotkey snippet below
s := RegExReplace( s, ",|((?:[^()]++|(?R))*)", "|$0|" )
s := StrReplace( s, "|,|" , ",`r`n" . A_Tab )
s := StrReplace( s, "|" , "")

正则表达式替换示例

这篇关于正则表达式替换嵌套括号匹配内的字符,或仅替换匹配外的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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