我正在对正则表达式进行练习,我真的不确定如何做到这一点。

问题描述 投票:0回答:3
,但是如果这是正确的,那么最后一个

(a ∪ b)*

实际上只是一个重复,所以我认为可以简化整个表达式以简化为

∪ (a*)

这似乎是正确的吗?
Edit:∪代表工会

	
您是对的。
(ab)*.
可以匹配A和B的任何字符串,因此可以匹配,因此它们是等效的。
(a*b*)*
相交
(a U b)*
是IS

(a U b)*

a*
regex simplification
3个回答
4
投票
a*

的子集。因此,可以将整个表达式简化为

a*

(a*
b*
+a
* =(a+b)*
using(a+b)*
=(a
* b)* a*
我们可以写
(a

* b)* a* + a*

0
投票
给我们(

(a* b)* + epsilon)a* 等于(a

* b)* a* i.e(a+b)*

真正的含义是(从

Herey复制) (a U b)*

目前,该表达式与所有这些序列匹配:(a U b)*(看

Here

没有删除捕获的组和剩余的字母顺序,因此无法简化这一点。

Edit:如果在您的正则语句中代表“联合”,则此表达式无效。不可能在正则没有任何东西。只有

-2
投票
,您需要使用

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 aabbaabbUaaU

,那么它可能是
OR
,但它仍然会匹配类似的东西。

,因为,在您的正则语句中捕获组是没有用的,因此类似的东西足以匹配任何数量的

|
((a*)(b*))*'s.

s。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.