How to tell if one regular expression matches a subset of another regular expression?

前端 未结 7 2263
孤独总比滥情好
孤独总比滥情好 2020-12-01 14:23

I\'m just wondering if it\'s possible to use one regular expression to match another, that is some sort of:

[\'a-z\'].match([\'b-x\'])
True

[\'m-n\'].match(         


        
7条回答
  •  醉梦人生
    2020-12-01 15:11

    You should do something along these lines:

    re.match("\[[b-x]-[b-x]\]", "[a-z]")
    

    The regular expression has to define what the string should look like. If you want to match an opening square bracket followed by a letter from b to x, then a dash, then another letter from b to x and finally a closing square bracket, the solution above should work.

    If you intend to validate that a regular expression is correct you should consider testing if it compiles instead.

提交回复
热议问题