Regular expression listing all possibilities

前端 未结 9 1451
感情败类
感情败类 2020-12-06 01:49

Given a regular expression, how can I list all possible matches? For example: AB[CD]1234, I want it to return a list like: ABC1234 ABD1234

I searched the web, but co

9条回答
  •  我在风中等你
    2020-12-06 02:06

    It's possible to write an algorithm to do this but it will only work for regular expressions that have a finite set of possible matches. Your regexes would be limited to using:

    • Optional: ?
    • Characters: . \d \D
    • Sets: like [1a-c]
    • Negated sets: [^2-9d-z]
    • Alternations: |
    • Positive lookarounds

    So your regexes could NOT use:

    • Repeaters: * +
    • Word patterns: \w \W
    • Negative lookarounds
    • Some zero-width assertions: ^ $

    And there are some others (word boundaries, lazy & greedy quantifiers) I'm not sure about yet.

    As for the algorithm itself, another user posted a link to this answer which describes how to create it.

提交回复
热议问题