gpt4 book ai didi

python - 可被 3 整除的正则表达式过滤器编号

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:48 28 4
gpt4 key购买 nike

我有一个逗号分隔的 ids(digits) 列表。我只需要得到能被 3 整除的那些。

示例:
i = "3454353, 4354353, 345352, 2343242, 2343242 ..."

最佳答案

只是为了它:

reobj = re.compile(
r"""\b # Start of number
(?: # Either match...
[0369]+ # a string of digits 0369
| # or
[147] # 1, 4 or 7
(?: # followed by
[0369]*[147] # optional 0369s and one 1, 4 or 7
[0369]*[258] # optional 0369s and one 2, 4 or 8
)* # zero or more times,
(?: # followed by
[0369]*[258] # optional 0369s and exactly one 2, 5 or 8
| # or
[0369]*[147] # two more 1s, 4s or 7s, with optional 0369s in-between.
[0369]*[147]
)
| # or the same thing, just the other way around,
[258] # this time starting with a 2, 5 or 8
(?:
[0369]*[258]
[0369]*[147]
)*
(?:
[0369]*[147]
|
[0369]*[258]
[0369]*[258]
)
)+ # Repeat this as needed
\b # until the end of the number.""",
re.VERBOSE)
result = reobj.findall(subject)

将查找字符串中所有可被 3 整除的数字。

关于python - 可被 3 整除的正则表达式过滤器编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10992279/

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