How to determine if a regex is orthogonal to another regex?

后端 未结 13 560
余生分开走
余生分开走 2020-12-13 16:40

I guess my question is best explained with an (simplified) example.

Regex 1:

^\\d+_[a-z]+$

Regex 2:

^\\d*$
<         


        
13条回答
  •  一个人的身影
    2020-12-13 16:53

    I finally found exactly the library that I was looking for:

    dk.brics.automaton

    Usage:

    /**
     * @return true if the two regexes will never both match a given string
     */
    public boolean isRegexOrthogonal( String regex1, String regex2 ) {
       Automaton automaton1 = new RegExp(regex1).toAutomaton();
       Automaton automaton2 = new RegExp(regex2).toAutomaton();
       return automaton1.intersection(automaton2).isEmpty();
    }
    

    It should be noted that the implementation doesn't and can't support complex RegEx features like back references. See the blog post "A Faster Java Regex Package" which introduces dk.brics.automaton.

提交回复
热议问题