How to show which sub-regex failed to match?

后端 未结 1 1683
后悔当初
后悔当初 2021-01-22 04:47

I am using regular expressions to validate user input. The following code collects a matches accessible with theMatch.Groups[\"identifier\"]. How can i get a list of sub-strings

相关标签:
1条回答
  • 2021-01-22 05:29

    Are you asking how to determine which specific capture group failed to match? To my knowledge once the match fails you won't be able to extract such information. If it fails it fails; no partial matching attempt info can be retrieved. What you could do is apply the entire regex as is to check the pattern in the desired order. Then, if it fails, try each part of your regex separately and tell the user which one failed (time, ip, site). This approach may make sense in this scenario, but might not work for all types of patterns.

    Regarding references, here are a few links:

    • .Net Regex Resources Reference (MSDN forums, various assortment of links)
    • http://www.regular-expressions.info/
    • The 30 Minute Regex Tutorial - nice tutorial, author of the Expresso tool
    • Expresso tool - good tool to test your regex in, has some samples and the above tutorial included. For other tools check out the MSDN link above.
    • Scott Hanselman's 2009 tool list - lists a couple of regex links

    If you're looking for a good book then the most popular is Jeffrey Friedl's Mastering Regular Expressions. A recent book that has positive ratings is the Regular Expressions Cookbook.

    0 讨论(0)
提交回复
热议问题