Reorder Regex Match Groups

2019-07-30 08:21发布

问题:

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.

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+)\.$

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

回答1:

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

Better (less backtracking on failure):

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