slaiyer
slaiyer

Reputation: 464

Ignore substring in regex match

I'm writing a function list parser for Notepad++ to add support for Nasal.

I'm using the following regular expression:

^[\t ]*(var[\n\s]+)?([_A-Za-z]?[\w_]*)[\n\s]*=[\n\s]*func[\n\s]*(\([^\)\(]*\))?: Debuggex Demo

... to match these 2 types of valid declarations:

  1. foo = func...
  2. var foo = func...

To further extract the function name foo from the matches of this regex, the best I could come up with was:

(var[\n\s]+)?([_A-Za-z]?[\w_]*): Debuggex Demo

Using this matches "foo" or "var foo", as the case may be. What is a regex that can ignore the presence of the substring var[\n\s]+], so it only extracts "foo" for the function list?

Thanks a lot.

Upvotes: 0

Views: 259

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89584

You can use the \K feature that removes all on the left from the match result:

(?:var\s+)?\K([A-Za-z_]\w*)\s*=\s*([A-Za-z_]\w*)

(Note that this feature is only available in recent versions of np++)

Upvotes: 3

Braj
Braj

Reputation: 46871

Get the matched group from index 2.

(\bvar\b)?\s\s*(\w+)\s\s*=\s\s*func\b

Online demo

Upvotes: 0

Related Questions