jasonscript
jasonscript

Reputation: 6170

Sublime SQL REGEX highlighting

I'm trying to modify an existing language definition in sublime

It's for SQL

Currently (\@|@@)(\w)* is being used to match against local declared parameters (e.g. @MyParam) and also system parameters (e.g. @@Identity or @@FETCH_STATUS)

I'm trying to split these into 2 groups

System parameters I can get like (\@\@)(\w)* but I'm having problems with the local parameters.

Can someone help me with the correct regex?

Upvotes: 0

Views: 103

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174726

Try the below regex to capture local parameters and system parameters into two separate groups.

(?<=^| )(@\w*)(?= |$)|(?<=^| )(@@\w*)(?= |$)

DEMO

Update:

Sublime text 2 supports \K(discards the previous matches),

(?m)(?:^| )\K(@\w*)(?= |$)|(?:^| )\K(@@\w*)(?= |$)

DEMO

enter image description here

Explanation:

(?m)                     set flags for this block (with ^ and $
                         matching start and end of line) (case-
                         sensitive) (with . not matching \n)
                         (matching whitespace and # normally)
(?:                      group, but do not capture:
  ^                        the beginning of a "line"
 |                        OR
                           ' '
)                        end of grouping
\K                       '\K' (resets the starting point of the
                         reported match)
(                        group and capture to \1:
  @                        '@'
  \w*                      word characters (a-z, A-Z, 0-9, _) (0 or
                           more times)
)                        end of \1
(?=                      look ahead to see if there is:
                           ' '
 |                        OR
  $                        before an optional \n, and the end of a
                           "line"
)                        end of look-ahead
|                        OR
(?:                      group, but do not capture:
  ^                        the beginning of a "line"
 |                        OR
                           ' '
)                        end of grouping
\K                       '\K' (resets the starting point of the
                         reported match)
(                        group and capture to \2:
  @@                       '@@'
  \w*                      word characters (a-z, A-Z, 0-9, _) (0 or
                           more times)
)                        end of \2
(?=                      look ahead to see if there is:
                           ' '
 |                        OR
  $                        before an optional \n, and the end of a
                           "line"

Upvotes: 1

Related Questions