gpt4 book ai didi

java - 正则表达式二进制字符串至少包含并以以下结尾

转载 作者:行者123 更新时间:2023-12-01 14:19:33 24 4
gpt4 key购买 nike

问题涉及为二进制字符串编写正则表达式。

如何编写一个以 00 结尾的代码,以及另一个包含至少三个 1 的代码?

最佳答案

00结尾:

^[01]*00$

包含至少三个1:

^(?=.*(?:0*1){3})[01]*$

两者:

^(?=.*(?:0*1){3})[01]*00$

说明:

^        # Start of string
(?= # Assert that the following could be matched here:
.* # Any number of characters
(?:0*1) # followed by any number of 0s and exactly one 1
{3} # three times
) # End of lookahead
[01]* # Match any number of ones or zeroes
00 # Match two zeroes
$ # at the end of the string

由于字符串可能只包含 1 和 0,因此我们实际上不需要向前查找。以下内容也适用(但仅在这些具体情况下):

^(?:0*1){3}[01]*00$

关于java - 正则表达式二进制字符串至少包含并以以下结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17738918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com