gpt4 book ai didi

python - 检查 python 字符串格式?

转载 作者:太空狗 更新时间:2023-10-29 19:32:45 26 4
gpt4 key购买 nike

我有一堆字符串,但我只想保留这种格式的字符串:

x/x/xxxxxx:xx

检查字符串是否符合这种格式的最简单方法是什么? (假设我想检查它是否有 2 个/'s 和一个 ':' )

最佳答案

尝试使用正则表达式:

import re
r = re.compile('.*/.*/.*:.*')
if r.match('x/x/xxxx xx:xx') is not None:
print 'matches'

你可以调整表达式来满足你的需要

关于python - 检查 python 字符串格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14966647/

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