具有偶数个 a 和奇数个 b 的字符串的正则表达式

发布于 2024-09-18 16:23:45 字数 102 浏览 10 评论 0原文

我在解决问题时遇到问题:- 它是一个作业,我解决了它,但它似乎太长和模糊,请任何人帮助我......

具有偶数个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 技术交流群。

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

发布评论

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

评论(13

只想待在家 2024-09-25 16:23:45

一种方法是通过两个正则表达式传递它,确保它们都匹配(假设您想使用正则表达式,请参阅下面的替代方案):

^b*(ab*ab*)*$
^a*ba*(ba*ba*)*$

任何其他(事实上, ,即使如此)很可能只是一种聪明的尝试,通常会导致巨大的失败。

第一个正则表达式确保混合中任何位置(之前、之后和之间)都有偶数个 ab

第二个类似,但通过起始 a*ba* 确保存在b奇数 个。


一种更好的方法是完全忽略正则表达式并简单地运行字符串,如下所示:

def isValid(s):
    set evenA to true
    set oddB to false
    for c as each character in s:
        if c is 'a':
            set evenA to not evenA
        else if c is 'b':
            set oddB to  not oddB
        else:
            return false
    return evenA and oddB

虽然正则表达式是一个很棒的工具,但它们并不适合所有情况,并且变得不太有用因为它们的可读性和可维护性下降。


就其价值而言,单一正则表达式的答案是:

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

但是,如果我发现我的团队中有人实际上使用了这样的怪物,他们就会被送回去再做一次。

这来自格雷格·培根的一篇论文。请参阅此处了解实际的内部工作原理。

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):

^b*(ab*ab*)*$
^a*ba*(ba*ba*)*$

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 with b 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 starting a*ba*.


A far better way to do it is to ignore regular expressions altogether and simply run through the string as follows:

def isValid(s):
    set evenA to true
    set oddB to false
    for c as each character in s:
        if c is 'a':
            set evenA to not evenA
        else if c is 'b':
            set oddB to  not oddB
        else:
            return false
    return evenA and oddB

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:

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

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.

兲鉂ぱ嘚淚 2024-09-25 16:23:45
Even-Even = (aa+bb+(ab+ba)(aa+bb)*(ab+ba))*

(Even-Even 有偶数个 Aas 和 b's)

偶数 a 和奇数 b = Even-Even b Even-Even

这应该有效

Even-Even = (aa+bb+(ab+ba)(aa+bb)*(ab+ba))*

(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

撩起发的微风 2024-09-25 16:23:45

这个正则表达式采用所有具有偶数个 a 和偶数个 b 的字符串

r1=((ab+ba)(aa+bb)*(ab+ba)+(aa+bb))*

现在得到偶数 a 和奇数 b 的正则表达式

r2=(b+a(aa+bb)*(ab+ba))((ab+ba)(aa+bb)*(ab+ba)+(aa+bb))*

This regular expression takes all strings with even number of a's and even number of b's

r1=((ab+ba)(aa+bb)*(ab+ba)+(aa+bb))*

Now to get regular expression for even a's and odd b's

r2=(b+a(aa+bb)*(ab+ba))((ab+ba)(aa+bb)*(ab+ba)+(aa+bb))*
秋叶绚丽 2024-09-25 16:23:45
  1. (bb)*a(aa)*ab(bb)*
  2. ab(bb)* a(aa)*
  3. b(aa)*(bb)*
    .
    .
    .
    .
    .

这样的正则表达式可以有很多。您是否还有其他条件,例如“以 a 开头”或类似的情况(奇数“b”和偶数“a”除外)?

  1. (bb)*a(aa)*ab(bb)*
  2. ab(bb)* a(aa)*
  3. 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') ?

不顾 2024-09-25 16:23:45

对于偶数个 a 和 b,我们有正则表达式:

E = { (ab + ba) (aa+bb)* (ab+ba) }*

对于偶数个 a 和奇数个 b,我们需要做的就是添加一个上述表达式 E 中额外的 b

所需的正则表达式将是:

E = { ((ab + ba) (aa+bb)* (ab+ba))* b ((ab + ba) (aa+bb)* (ab+ba))* }

For even number of a's and b's , we have regex:

E = { (ab + ba) (aa+bb)* (ab+ba) }*

For even number of a's and odd number of b's , all we need to do is to add an extra b in the above expression E.

The required regex will be:

E = { ((ab + ba) (aa+bb)* (ab+ba))* b ((ab + ba) (aa+bb)* (ab+ba))* }
为你拒绝所有暧昧 2024-09-25 16:23:45

我会这样做:

  • 正则表达式 even 匹配符号 a,然后匹配 b 序列,然后匹配符号 a<再次 /em>,然后是另一个 b 序列,这样就有偶数个 b

偶数 -> (a (bb)* a (bb)* | a b (bb)* a b (bb)*)

  • 正则表达式 奇数 对奇数个 b 执行相同的操作:

奇数 -> (a b (bb)* a (bb)* | a (bb)* a b (bb)*)

偶数字符串的 a 和奇数的 b 之一:

  • 以奇数的 b 开头,后跟偶数偶数模式中奇数模式的数量;
  • or 以偶数个 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:

  • regex even matches the symbol a, then a sequence of b's, then the symbol a again, then another sequence of b's, such that there is an even number of b's:

even -> (a (bb)* a (bb)* | a b (bb)* a b (bb)*)

  • regex odd does the same with an odd total number of b's:

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:

  • starts with an odd number of b's, and is followed by an even number of odd patterns amongst even patterns;
  • or starts with an even number of b's, and is followed by an odd number of odd patterns amongst even patterns.

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.

墟烟 2024-09-25 16:23:45

高级建议:为该语言构造一个确定性有限自动机——非常简单,对状态中的 ab 数量进行奇偶编码, q0 编码偶数 nr。 as 甚至 nr。 b ,并相应地转换 ---,然后将 DFA 转换为正则表达式(通过使用众所周知的算法或“从头开始”)。

这里的想法是利用 DFA(正则语言的算法描述)和正则表达式(正则语言的代数描述)之间易于理解的等价性。

A high-level advice: construct a deterministic finite automaton for the language---very easy, encode parity of the number of as and bs in the states, with q0 encoding even nr. of as and even nr. of bs, 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).

挽梦忆笙歌 2024-09-25 16:23:45

正则表达式如下:

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

The regular expression are given below :

    (aa|bb)*((ab|ba)(aa|bb)*(ab|ba)(aa|bb)*b)*
情感失落者 2024-09-25 16:23:45

实现此目的的结构化方法是制作一个转换图并从中构建正则表达式。
这种情况下的正则表达式

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

看起来很复杂,但它涵盖了所有可能出现的情况。

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

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

It looks complicated but it covers all possible cases that may arise.

拥抱没勇气 2024-09-25 16:23:45

答案是 (aa+ab+ba+bb)* b (aa+ab+ba+bb)*

the answer is (aa+ab+ba+bb)* b (aa+ab+ba+bb)*

长伴 2024-09-25 16:23:45

(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.

债姬 2024-09-25 16:23:45

如果是偶数个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

梓梦 2024-09-25 16:23:45

所有具有偶数个 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.

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