检查python字符串格式? [英] Check python string format?
本文介绍了检查python字符串格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一堆字符串,但我只想保留以下格式:
I have a bunch of strings but I only want to keep the ones with this format:
x / x / xxxx xx:xx
x/x/xxxx xx:xx
检查字符串是否符合此格式的最简单方法是什么? (假设我要检查它是否具有2 /以及一个':')
What is the easiest way to check if a string meets this format? (Assuming I want to check by if it has 2 /'s and a ':' )
推荐答案
请尝试按常规表达式进行尝试:
try with regular expresion:
import re
r = re.compile('.*/.*/.*:.*')
if r.match('x/x/xxxx xx:xx') is not None:
print 'matches'
您可以调整表达式以符合您的需求
you can tweak the expression to match your needs
这篇关于检查python字符串格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文