C#C
C#4y ago
Ezlanding

Best way to implement a regex based lexer [Answered]

In a regex lexer, you can loop over every pattern and do something like this:
if (match.Success && (match.Index - currentIndex) == 0)
  //Token Found

The problem with this is that, unlike other Regex implementations, if c# regex sees that the first char does not match the pattern, instead of returning null/false, it keeps going until a match is found, meaning the if statement is needed and extra match calls are made.
Is there any way to get this to work without the extra calls? I could use the index of each match and add them into a list in order based off the match's index position, but that seems needlessly complicated if there is a better way
Was this page helpful?