5

我正在做有关正则表达式的练习,但我真的不确定如何做到这一点。

正则表达式是:

((a*)(b*))* ∪ (a*)

我真的不擅长这个,但我认为((a*)(b*))*可以简化为(a ∪ b)*但是如果这是对的,那么最后一个∪ (a*)实际上只是重复,所以我认为整个表达式可以简化为(a ∪ b)*.这似乎正确吗?

编辑:∪代表联合

4

2 回答 2

4

你说的对。(a*b*)*可以匹配 a 和 b 的任何字符串,所以 can (a U b)*,因此它们是等价的。(a U b)*intersecta*也是的a*a*(a U b)*。因此,整个表达式可以简化为(a U b)*

于 2014-05-09T14:10:47.507 回答
-2

真正的意思是(从这里((a*)(b*))*U(a*)复制)

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  (                        group and capture to \1 (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (                        group and capture to \2:
--------------------------------------------------------------------------------
      a*                       'a' (0 or more times (matching the
                               most amount possible))
--------------------------------------------------------------------------------
    )                        end of \2
--------------------------------------------------------------------------------
    (                        group and capture to \3:
--------------------------------------------------------------------------------
      b*                       'b' (0 or more times (matching the
                               most amount possible))
--------------------------------------------------------------------------------
    )                        end of \3
--------------------------------------------------------------------------------
  )*                       end of \1 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
--------------------------------------------------------------------------------
  U                        'U'
--------------------------------------------------------------------------------
  (                        group and capture to \4:
--------------------------------------------------------------------------------
    a*                       'a' (0 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
  )                        end of \4

这个表达式当前匹配所有这些序列:(abUa bU U aabbUaa aaUaa aaU Uaa bbU ababUaa aabbaabbUaa这里

如果不删除捕获组和剩余的字母顺序,就无法简化这一点。

编辑:如果U在你的正则表达式中代表“联合”,那么这个表达式是无效的。没有办法在正则表达式中联合任何东西。只有OR并且您需要为此使用|(管道)。如果你想联合((a*)(b*))*(a*)那么它可能会((a*)(b*))*,但它仍然会匹配任何类似的东西abaab

尽管如此,在你的正则表达式语句中捕获组是没有用的,所以类似的东西[ab]*足以匹配任意数量的a's 和b's。

于 2014-05-09T14:22:14.060 回答