具有偶数个 a 和奇数个 b 的字符串的正则表达式
我在解决问题时遇到问题:- 它是一个作业,我解决了它,但它似乎太长和模糊,请任何人帮助我......
具有偶数个a和奇数个b的字符串的正则表达式,其中字符集= { a,b}。
Im having a problem in solving the problem:-
Its an assignment, i solved it, but it seems to be too long and vague, Can anyboby help me please......
Regular expression for the strings with even number of a's and odd number of b's where the character set={a,b}.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
一种方法是通过两个正则表达式传递它,确保它们都匹配(假设您想使用正则表达式,请参阅下面的替代方案):
任何其他(事实上, ,即使如此)很可能只是一种聪明的尝试,通常会导致巨大的失败。
第一个正则表达式确保混合中任何位置(之前、之后和之间)都有偶数个
a
和b
。第二个类似,但通过起始
a*ba*
确保存在b
的奇数 个。一种更好的方法是完全忽略正则表达式并简单地运行字符串,如下所示:
虽然正则表达式是一个很棒的工具,但它们并不适合所有情况,并且变得不太有用因为它们的可读性和可维护性下降。
就其价值而言,单一正则表达式的答案是:
但是,如果我发现我的团队中有人实际上使用了这样的怪物,他们就会被送回去再做一次。
这来自格雷格·培根的一篇论文。请参阅此处了解实际的内部工作原理。
One way to do this is to pass it through two regular expressions making sure they both match (assuming you want to use regular expressions at all, see below for an alternative):
Anything else (and, in fact, even that) is most likely just an attempt to be clever, one that's generally a massive failure.
The first regular expression ensures there are an even number of
a
withb
anywhere in the mix (before, after and in between).The second is similar but ensures that there's an odd number of
b
by virtue of the startinga*ba*
.A far better way to do it is to ignore regular expressions altogether and simply run through the string as follows:
Though regular expressions are a wonderful tool, they're not suited for everything and they become far less useful as their readability and maintainability degrades.
For what it's worth, a single-regex answer is:
but, if I caught anyone on my team actually using a monstrosity like that, they'd be sent back to do it again.
This comes from a paper by one Greg Bacon. See here for the actual inner workings.
(Even-Even 有偶数个 Aas 和 b's)
偶数 a 和奇数 b = Even-Even b Even-Even
这应该有效
(Even-Even has even number of Aas and b's both)
Even a's and odd b's = Even-Even b Even-Even
This hsould work
这个正则表达式采用所有具有偶数个 a 和偶数个 b 的字符串
现在得到偶数 a 和奇数 b 的正则表达式
This regular expression takes all strings with even number of a's and even number of b's
Now to get regular expression for even a's and odd b's
(bb)*a(aa)*ab(bb)*
ab(bb)* a(aa)*
b(aa)*(bb)*
.
.
.
.
.
。
这样的正则表达式可以有很多。您是否还有其他条件,例如“以 a 开头”或类似的情况(奇数“b”和偶数“a”除外)?
(bb)*a(aa)*ab(bb)*
ab(bb)* a(aa)*
b(aa)*(bb)*
.
.
.
.
.
.
there can be many such regular expressions. Do you have any other condition like "starting with a" or something of the kind (other than odd 'b' and even 'a') ?
对于偶数个 a 和 b,我们有正则表达式:
对于偶数个 a 和奇数个 b,我们需要做的就是添加一个上述表达式
E
中额外的b
。所需的正则表达式将是:
For even number of a's and b's , we have regex:
For even number of
a
's and odd number ofb
's , all we need to do is to add an extrab
in the above expressionE
.The required regex will be:
我会这样做:
偶数 -> (a (bb)* a (bb)* | a b (bb)* a b (bb)*)
奇数 -> (a b (bb)* a (bb)* | a (bb)* a b (bb)*)
偶数字符串的 a 和奇数的 b 之一:
请注意,even 与字符串中 a/b 的偶数/奇数无关。
正则表达式 ->
(
b (bb)* 偶数* (奇数 偶数* 奇数)* 偶数*
|
(bb)* 偶数* 奇数 偶数* (奇数 偶数* 奇数)* 偶数*
)
当然可以替换每一次出现的最终正则表达式中的偶数和奇数以获得单个正则表达式。
很容易看出,满足此正则表达式的字符串确实会有偶数个 a(因为符号 a 仅出现在 even 中)和奇数子正则表达式,并且每个子正则表达式都使用两个a)和奇数个b(第一种情况:1 >b + 偶数个b + 偶数个奇数 第二种情况:偶数个b +奇数奇数)。
具有偶数个 a 和奇数个 b 的字符串将满足此正则表达式,因为它以零个或多个 b 开头> 的,然后是 [一个 a、零个或多个 b、再一个 a 和零个或多个 b's],零次或多次。
I would do as follows:
even -> (a (bb)* a (bb)* | a b (bb)* a b (bb)*)
odd -> (a b (bb)* a (bb)* | a (bb)* a b (bb)*)
A string of even number of a's and odd number of b's either:
Note that even has no incidence on the evenness/oddness of the a/b's in the string.
regex ->
(
b (bb)* even* (odd even* odd)* even*
|
(bb)* even* odd even* (odd even* odd)* even*
)
Of course one can replace every occurence of even and odd in the final regex to get a single regex.
It is easy to see that a string satisfying this regex will indeed have an even number of a's (as symbol a occurs only in even and odd subregexes, and these each use exactly two a's) and an odd number of b's (first case : 1 b + even number of b's + even number of odd; second case : even number of b's + odd number of odd).
A string with an even number of a's and an odd number of b's will satisfy this regex as it starts with zero or more b's, then is followed by [one a, zero or more b's, one more a and zero or more b's], zero or more times.
高级建议:为该语言构造一个确定性有限自动机——非常简单,对状态中的
a
和b
数量进行奇偶编码,q0
编码偶数 nr。a
s 甚至 nr。 b ,并相应地转换 ---,然后将 DFA 转换为正则表达式(通过使用众所周知的算法或“从头开始”)。这里的想法是利用 DFA(正则语言的算法描述)和正则表达式(正则语言的代数描述)之间易于理解的等价性。
A high-level advice: construct a deterministic finite automaton for the language---very easy, encode parity of the number of
a
s andb
s in the states, withq0
encoding even nr. ofa
s and even nr. ofb
s, and transition accordingly---, and then convert the DFA into a regular expression (either by using well-known algorithms for this or "from scratch").The idea here is to exploit the well-understood equivalence between the DFA (an algorithmic description of regular languages) and the regular expressions (an algebraic description of regular languages).
正则表达式如下:
The regular expression are given below :
实现此目的的结构化方法是制作一个转换图并从中构建正则表达式。
这种情况下的正则表达式
看起来很复杂,但它涵盖了所有可能出现的情况。
The structured way to do it is to make one transition diagram and build the regular expression from it.
The regex in this case will be
It looks complicated but it covers all possible cases that may arise.
答案是 (aa+ab+ba+bb)* b (aa+ab+ba+bb)*
the answer is (aa+ab+ba+bb)* b (aa+ab+ba+bb)*
(bb)* b (aa)* + (aa)* b (bb)*
这是处理所有带有奇数 b 和偶数 a 的字符串的答案。
(bb)* b (aa)* + (aa)* b (bb)*
This is the answer which handles all kind of strings with odd b's and even a's.
如果是偶数个a后面跟着奇数个b
应该有效
(aa)*b(bb)*如果按任何顺序都
(aa)*b(bb)* + b(bb)(aa) 应该可以
If it is even number of a's followed by odd number of b's
(aa)*b(bb)* should work
if it is in any order
(aa)*b(bb)* + b(bb)(aa) should work
所有具有偶数个 a 和奇数个 b 的字符串
(((aa+bb) * b(aa+bb) * ) + (A +((a+b)b(a+b)) *)) *
这里 A 代表空字符串。 A可以忽略不计。
如果有任何错误请指出。
All strings that have even no of a's and odd no of b's
(((aa+bb) * b(aa+bb) * ) + (A +((a+b)b(a+b)) *)) *
here A is for null string. A can be neglected.
if there is any error plz point it out.