Given a regular expression, how would I generate all strings that match it?

后端 未结 4 719
不思量自难忘°
不思量自难忘° 2020-12-15 00:07

I\'m using a simple language of only (), |, spaces, and alpha characters.
Given a regular expression like the following:

(hello         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 00:28

    When I did my own custom little language, I wrote a parser first. The parser created a structure in memory that represented the text. For this little language, I would create a structure that's something like this:

    Node:
      list of string values
      isRequired
      list of child Nodes
    

    When you parse your text, you would get a list of nodes:

       Node1:
          hello, goodbye
          true
          [] (no child nodes)
       Node2:
          world,
          false
          [
            Node3:
               s,
               false
               []
          ]
    

    Once you parse into this structure, you can imagine code that'll generate what you want given that you understand what must be include, and what may be included. The pseudo code would look like this

    recursiveGenerate( node_list, parital )
       if ( node_list is null or is empty )
          add partial to an output list
       for the first node
          if ( ! node.isRequired )
             recursiveGenrate( remaining nodes, partial )
          for each value
             recursiveGenerate( child Nodes + remaining nodes, partial + value )
    

    That should populate your list in the way you want.

提交回复
热议问题