Are there any good / interesting analogs to regular expressions in 2d?

后端 未结 4 1715
醉话见心
醉话见心 2021-01-31 17:54

Are there any good (or at least interesting but flawed) analogs to regular expressions in two dimensions?

In one dimension I can write something like /aaac?(bc)*b?

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-31 18:06

    Nice problem.

    First, ask yourself if you can constrain the pattern to a "+" pattern, or if you would it need to test/match rectangles also. For instance, a rectangle of [bc] with a border of a would match the center rectangle below, but would also match a "+" shape of [c([bc]*a})v([bc]*a)>([bc]*a)<([bc]*a)] (in your syntax)

    xxxaaaaaxxx
    yzyabcba12d
    defabcbass3
    oreabcba3s3
    s33aaaaas33
    k388x.egiee
    

    If you can constrain it to a "+" then your task is much easier. As ShuggyCoUk said, parsing a RE is usually equivalent to a DFSM -- but for a single, serial input which simplifies things greatly.

    In your "RE+" engine, you'll have to debug not only the engine, but also each place that the expressions are entered. I'd like the compiler to catch any errors it could. Given that, you could also use something that was explicitly four RE's, like:

    REPlus engine = new REPlus('b').North("a{3}")
       .East("a{3}").South("a{3}").West("a{3}");
    

    However, depending on your implementation this may be cumbersome.

    And with regard to the traversal engine -- would the North/West patterns match RtL or LtR? It could matter if the patterns match differently with or w/o greedy sub-matches.

    Incidentally, I think the '^' in your example is supposed to be one character to the left, outside the parenthesis.

提交回复
热议问题