Listing all patterns that a regex matches

左心房为你撑大大i 提交于 2020-01-24 07:51:26

问题


I am looking for a way to list all possible patterns from a finite regex (with no duplicates). Is there any source available?


回答1:


Although it won't cover some advanced features, and has its own share of other caveats, Regexp::Genex seems to be close to what you are looking for.

There's also this thread of PerlMonks which is relevant enough (as well as explaining how Regexp::Genex might not do for you, and some roll-yourself alternatives).

Otherwise, as per Jeffrey Friedl's Mastering Regular Expressions, you could use the /g modifier, coupled with the (?{CODE}) extension and a pattern that will never match, ala:

perl -E '$_ = 'Mastering Regular Expressions'; /(\p{L}*)(?{ say qq![$^N]! })(?!)/g;'



回答2:


A Haskell program based on Perl's Regexp::Genex can be found on Github and on Hackage.

According to the author, it was inspired by Regexp::Genex, but "uses a random-walk approach for character classes, instead of enumerating all possibilities."



来源:https://stackoverflow.com/questions/4605289/listing-all-patterns-that-a-regex-matches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!