Suriv
Suriv

Reputation: 35

Regex positive lookahead with backreference

I'm trying to build a very specific editor with syntax highlight. I've made all the simple stuff with regular expressions wich was simple enough, but now I would like to add some advanced features also with regular expressions.

In this case I would like to know if a defined tag is used on any JumpTo command or not.
Example:

Define1,Command,Command
Define2,Command,Command,JumpTo4
Define3,Command,Command,JumpTo1
Define4,Command,Command

The objective here would be to find every Define (Definexxx) that is referenced by a JumpTo (one or many) may it be before or after the definition. In another step I would also like to find any Define that is not referenced by any JumpTo.

I've tried the expression "Define(?<tag>\d+)(?=JumpTo\k<tag>)" for positive lookahead. I would expect that it would find "Define1" but it didn't (neither with nor without multiline option).

Is this possible to be done in one step Regex, or should I drop the idea and just consider a 2 step approach?

Upvotes: 2

Views: 1685

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336108

You were nearly there, just forgot a .*. And I've added a lookbehind assertion to also check before the current define:

Regex regexObj = new Regex(
    @"Define(?<tag>\d+\b)       # Match Define<number>
    (?:                         # Match either...
     (?=.*JumpTo\k<tag>\b)      # if JumpTo<number> occurs somewhere ahead 
    |                           # or...
     (?<=JumpTo\k<tag>\b.*)     # if JumpTo<number> occurs somewhere before 
    )                           # End of alternation", 
    RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);

I've added \b word boundary anchors to ensure that we're always matching the entire number (this gets important if we reach Define10 and above).

Also, please note the use of the Singleline option. This is what you want, not Multiline which refers to how ^ and $ match.

Upvotes: 8

Related Questions