重新排序正则表达式匹配组 [英] Reorder Regex Match Groups

查看:39
本文介绍了重新排序正则表达式匹配组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 grails 编写 Sublime Text 2 构建配置,其中包括一个正则表达式(perl 样式),用于将错误消息解析为文件、行、列和消息部分.错误来自以下格式的 grails CLI:

I am writing a Sublime Text 2 build configuration for grails, which includes a regular expression (perl style) to parse error messages in to file, line, column, and message parts. The errors come from the grails CLI in the following format:

{Project_Directory}/SourceFile.groovy: 19: errror_message @ line 19, column 5.

我当前的正则表达式匹配所有四个部分,但 Sublime 似乎要求匹配按顺序发生,即匹配组 1 = 文件名,2 = 行号,3 = 列号,4 = 错误消息.Grails 以相反的顺序报告第 3 项和第 4 项,因此我需要编写一个正则表达式,将列号放在匹配组 3 中,将错误消息放在组 4 中.我当前的正则表达式(匹配,但不反转组 3和 4) 如下:

My current regex matches all four parts, but Sublime seems to require that the matches occur in order, that is match group 1 = file name, 2 = line number, 3 = column number, 4 = errror message. Grails is reporting items 3 and 4 in reverse order, so I need to write a regex that will put the column number in match group 3 and the error message in group 4. My current regex (which matches, but doesn't reverse groups 3 and 4) is as follows:

^(.+?): (\d+): (.+?) \@ line \d+, column (\d+)\.$

有什么想法吗?这甚至可能吗?有人知道 sublime 是否会接受命名组而不是编号组?

Any ideas? Is this even possible? Does anybody know if sublime will accept named groups instead of numbered groups?

推荐答案

^(.+?): (\d+): (?=.+? \@ line \d+, column (\d+)\.$)(.+?) \@

更好(更少的失败回溯):

Better (less backtracking on failure):

^([^:]+): (\d+): (?=[^@]+ \@ line \d+, column (\d+)\.$)([^@]+) \@

这篇关于重新排序正则表达式匹配组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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