是否可以进一步简化这个正则表达式?

发布于 2024-08-05 19:32:03 字数 327 浏览 2 评论 0原文

我正在为我的编译器类做一些家庭作业,但遇到以下问题:

为包含 ab 的所有字符串编写正则表达式奇数个 a 或奇数个 b(或两者)。

经过大量的白板工作后,我想出了以下解决方案:

(aa|bb)* (ab|ba|a|b) ((aa|bb)* (ab|ba) (aa|bb)* (ab|ba) (aa|bb)*)*

但是,这是我能得到的最简化的解决方案吗?我考虑过构建 DFA,试图尽量减少那里的状态数量,看看它是否能帮助我简化,但我想我应该先向正则表达式专家询问。

I'm working on some homework for my compiler class and I have the following problem:

Write a regular expression for all strings of a's and b's that contain an odd number of a's or an odd number of b's (or both).

After a lot of whiteboard work I came up with the following solution:

(aa|bb)* (ab|ba|a|b) ((aa|bb)* (ab|ba) (aa|bb)* (ab|ba) (aa|bb)*)*

However, Is this is the most simplified I can get it? I've considered constructing the DFA trying to minimize the number of states there to see if it would help me simplify but I figured I would ask the regex gurus on SO first.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假扮的天使 2024-08-12 19:32:03

采纳 Greg D 的建议,即从 a(aa)* 开始,然后从那里开始。 Sepp2k 几乎是正确的,但真正的考虑是你不关心另一个字母。我的意思是,当您查看“a 的奇数”约束时,您根本不关心字符串中的 b 是什么。因此,将 b* 粘贴在任何可以的地方:)

Sepp2k 的答案几乎是正确的,但这个是正确的:

b* a b* (a b* a b* )* | a* b a* (b a* b a* )*

详细来说,这个正则表达式计算出所有具有奇数个 a 的字符串(第一部分),并将这些字符串与任何包含奇数个 b 的字符串。

Take Greg D's recommendation of starting with a(aa)*, and going from there. Sepp2k almost has it right, but the real consideration is that you don't care about the other letter. What I mean is, when you are looking at the "odd number of a's" constraint, you don't care at all about what b's are in your string. Thus, stick b*'s anywhere you can :)

Sepp2k's answer is almost right, but this one is correct:

b* a b* (a b* a b* )* | a* b a* (b a* b a* )*

To elaborate, this regex figures out all strings with an odd number of a's (first section), and OR's those strings with any strings containing an odd number of b's.

药祭#氼 2024-08-12 19:32:03

这应该有效:

b* a b* (a b* a b*)* |  a* b a* (b a* b a*)*

This should work:

b* a b* (a b* a b*)* |  a* b a* (b a* b a*)*
梦萦几度 2024-08-12 19:32:03

恐怕我不相信你写的正则表达式是正确的。考虑字符串:

aba

我们有几个匹配选择,但事实上它是奇数长度,这意味着我们必须在前面匹配一个单独的 a,所以:

(a)(ba)

但是,遗憾的是,你的第二个主要分组不可能匹配 (ba) 。

当处理这样的约束时,我发现从核心约束开始并从那里开始更容易。在这种情况下,您的约束是“奇数”,因此首先

a(aa)*

强制使用奇数个a,然后从那里开始。 :)

I'm afraid that I don't believe your regex as written is correct. Consider the string:

aba

We have a couple choices for matches, but the fact that it's odd-length means we must match a lone a at the front, so:

(a)(ba)

But, sadly, it's impossible for your second main grouping there to match (ba).

When dealing with a constraint like this, I found it easier to start from the core constraint and go from there. In this case, your constraint is "odd," so start with

a(aa)*

to force an odd number of a's and go from there. :)

涫野音 2024-08-12 19:32:03

我认为你需要以不同的方式处理这个问题。

您正在尝试匹配 ab 均不为偶数的任何内容。

从匹配偶数ab的东西开始可能会更容易。此时您所要做的就是在末尾添加一些与您实际想要匹配的最小字符串相匹配的内容。

I think you need to approach the problem differently.

You are trying to match anything that doesn't have even numbers of both a and b.

It would probably be easier to start with something that matches even numbers of a and b. All you have to do at that point would be to add something on the end that matches the smallest string that you actually want to match.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文