© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
25 replies
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
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

regex help [Answered]
C#CC# / help
4y ago
Best way to serialize a date without a year [Answered]
C#CC# / help
4y ago
❔ 21,000 Images, Best way to implement a lookup method?
C#CC# / help
4y ago
multiple capture groups regex [Answered]
C#CC# / help
4y ago