Chris
Chris

Reputation: 97

Sublime Regex Variable Handling?

completely stumped on this one;

trying to recognize this: x content end_x

I'm trying to get sublime to recognize for some specific functions for a programming language - for example, I'm trying to get the first bit of the function to be recognized (which could be any string x) then have the full function (including any characters). The only defining feature of the function is the closing statement "end_", followed by the same string x from the opening statement.

I'm just wondering if this is possible? If so, how?


I should probably add - I'd really only want to highlight x and end_x, not particularly the content content

Upvotes: 1

Views: 363

Answers (1)

zx81
zx81

Reputation: 41838

Use this regex:

(?s)(\S+) .*? end_\1

See the sample matches in the regex demo.

Explanation

  • (?s) activates DOTALL mode, allowing the dot to match across lines
  • The parentheses in (\S+) capture any chars that are not white-space chars
  • .*? lazily matches up to...
  • end_ literal chars
  • \1 the content that was matched by Group 1

Upvotes: 1

Related Questions