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

后端 未结 13 529
余生分开走
余生分开走 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 17:12

    I would do the following:

    • convert each regex to a FSA, using something like the following structure:

      struct FSANode
      {
          bool accept;
          Map<char, FSANode> links;
      }
      List<FSANode> nodes;
      FSANode start;
      

    Note that this isn't trivial, but for simple regex shouldn't be that difficult.

    • Make a new Combined Node like:

      class CombinedNode
      {
          CombinedNode(FSANode left, FSANode right)
          {
              this.left = left;
              this.right = right;
          }
      
          Map<char, CombinedNode> links;
          bool valid { get { return !left.accept || !right.accept; } }
      
          public FSANode left;
          public FSANode right;
      }
      

    Build up links based on following the same char on the left and right sides, and you get two FSANodes which make a new CombinedNode.

    Then start at CombinedNode(leftStart, rightStart), and find the spanning set, and if there are any non-valid CombinedNodes, the set isn't "orthogonal."

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