使用正则表达式确定字符串是偶数还是奇数长度

时间:2022-06-04 02:25:05

I am having trouble building a regular expression with the set of strings over {a, b, c} that is an odd length with exactly one a. Here is my best attempt so far:

我在使用{a,b,c}上的字符串集构建正则表达式时遇到问题,这个字符串是一个奇数长度,只有一个a。到目前为止,这是我最好的尝试:

(bb|bc|cb|cc)*a(bb|bc|cb|cc)*

This does good for even b and c on either side of the a, but does not account for a odd b and c combination on either side of the a.

这对于a的两侧的偶数b和c都有好处,但是不考虑a的任一侧的奇数b和c组合。

Any hints?

任何提示?

1 个解决方案

#1


4  

Your string will be a prefix followed by a followed by a suffix.

您的字符串将是前缀,后跟后跟后缀。

Both prefix and suffix can be zero length. If not, they have to be either both even or both uneven. This means you have two main cases.

前缀和后缀都可以是零长度。如果不是,它们必须是均匀的或两者都是不均匀的。这意味着您有两个主要案例。

EVENPREFIX a EVENSUFFIX | UNEVENPREFIX a UNEVENSUFFIX

Try this (incomplete and wrong):

试试这个(不完整和错误):

([bc][bc])*a([bc][bc])*|([bc][bc][bc])*a([bc][bc][bc])*

There is still one uneven case missing: a single [bc]:

还有一个不平衡的案例缺失:一个[bc]:

(([bc][bc])*a([bc][bc])*)|([bc]([bc][bc])*a[bc]([bc][bc])*)

According to http://www.fileformat.info/tool/regex.htm, this matches

根据http://www.fileformat.info/tool/regex.htm,这匹配

  • a
  • 一个
  • cac
  • CAC
  • ccabb
  • ccabb

I expect it matches the rest too...

我希望它与其他的相匹配......

The left side guarantees even (or empty) sequences of b or c. The right side is either a single b or c followed by a multiple of two (so that it stays uneven).

左侧保证b或c的偶数(或空)序列。右侧是单个b或c,后跟两个的倍数(因此它保持不均匀)。

Kobi came up with this refinement of the above:

Kobi想出了上述的改进:

([bc][bc])*(a|[bc]a[bc])([bc][bc])*

How does this work?

这个怎么用?

The first group is guaranteed to be even. The second group is guaranteed to be uneven with a single a inside. The third group is guaranteed to be be even. Thus, the whole is guaranteed to be uneven.

第一组保证是平等的。第二组保证不均匀,单个内部。第三组保证是平等的。因此,整体保证不均匀。

#1


4  

Your string will be a prefix followed by a followed by a suffix.

您的字符串将是前缀,后跟后跟后缀。

Both prefix and suffix can be zero length. If not, they have to be either both even or both uneven. This means you have two main cases.

前缀和后缀都可以是零长度。如果不是,它们必须是均匀的或两者都是不均匀的。这意味着您有两个主要案例。

EVENPREFIX a EVENSUFFIX | UNEVENPREFIX a UNEVENSUFFIX

Try this (incomplete and wrong):

试试这个(不完整和错误):

([bc][bc])*a([bc][bc])*|([bc][bc][bc])*a([bc][bc][bc])*

There is still one uneven case missing: a single [bc]:

还有一个不平衡的案例缺失:一个[bc]:

(([bc][bc])*a([bc][bc])*)|([bc]([bc][bc])*a[bc]([bc][bc])*)

According to http://www.fileformat.info/tool/regex.htm, this matches

根据http://www.fileformat.info/tool/regex.htm,这匹配

  • a
  • 一个
  • cac
  • CAC
  • ccabb
  • ccabb

I expect it matches the rest too...

我希望它与其他的相匹配......

The left side guarantees even (or empty) sequences of b or c. The right side is either a single b or c followed by a multiple of two (so that it stays uneven).

左侧保证b或c的偶数(或空)序列。右侧是单个b或c,后跟两个的倍数(因此它保持不均匀)。

Kobi came up with this refinement of the above:

Kobi想出了上述的改进:

([bc][bc])*(a|[bc]a[bc])([bc][bc])*

How does this work?

这个怎么用?

The first group is guaranteed to be even. The second group is guaranteed to be uneven with a single a inside. The third group is guaranteed to be be even. Thus, the whole is guaranteed to be uneven.

第一组保证是平等的。第二组保证不均匀,单个内部。第三组保证是平等的。因此,整体保证不均匀。