Intersection of two regular expressions

后端 未结 4 1617
忘了有多久
忘了有多久 2020-12-17 18:05

Im looking for function (PHP will be the best), which returns true whether exists string matches both regexpA and regexpB.

Example 1:

$regexpA = \'[0         


        
4条回答
  •  死守一世寂寞
    2020-12-17 18:11

    For regular expressions that are actually regular (i.e. don't use irregular features like back references) you can do the following:

    1. Transform the regexen into finite automata (the algorithm for that can be found here(chapter 9) for example).
    2. Build the intersection of the automata (You have a state for each state in the cartesian product of the states of the two automata. You then transition between the states according to the original automata's transition rules. E.g. if you're in state x1y2, you get the input a, the first automaton has a transition x1->x4 for input x and the second automaton has y2->y3, you transition into the state x4y3).
    3. Check whether there's a path from the start state to the end state in the new automaton. If there is, the two regexen intersect, otherwise they don't.

提交回复
热议问题