Regular expression listing all possibilities

前端 未结 9 1435
感情败类
感情败类 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:17

    For some simple regular expressions like the one you provided (AB[CD]1234), there is a limited set of matches. But for other expressions (AB[CD]*1234) the number of possible matches are not limited.

    One method for locating all the posibilities, is to detect where in the regular expression there are choices. For each possible choice generate a new regular expression based on the original regular expression and the current choice. This new regular expression is now a bit simpler than the original one.

    For an expression like "A[BC][DE]F", the method will proceed as follows

    getAllMatches("A[BC][DE]F")
    = getAllMatches("AB[DE]F") + getAllMatches("AC[DE]F")
    = getAllMatches("ABDF") + getAllMatches("ABEF") 
       + getAllMatches("ACDF")+ getAllMatches("ACEF")
    = "ABDF" + "ABEF" + "ACDF" + "ACEF"
    
    0 讨论(0)
  • 2020-12-06 02:17

    Impossible.

    Really.

    Consider look ahead assertions. And what about .*, how will you generate all possible strings that match that regex?

    0 讨论(0)
  • 2020-12-06 02:20

    The reason you haven't found anything is probably because this is a problem of serious complexity given the amount of combinations certain expressions would allow. Some regular expressions could even allow infite matches:

    Consider following expressions:

    AB[A-Z0-9]{1,10}1234
    
    AB.*1234
    

    I think your best bet would be to create an algorithm yourself based on a small subset of allowed patterns. In your specific case, I would suggest to use a more naive approach than a regular expression.

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