use*_*000 10 python string data-manipulation
我有一堆字符串,但我只想保留这种格式:
x/x/xxxx xx:xx
检查字符串是否符合此格式的最简单方法是什么?(假设我想查看是否有2 /'和':')
kof*_*ann 18
尝试定期表达:
import re
r = re.compile('.*/.*/.*:.*')
if r.match('x/x/xxxx xx:xx') is not None:
print 'matches'
Run Code Online (Sandbox Code Playgroud)
你可以调整表达式以满足你的需求
如果您使用匹配的正则表达式,您还必须考虑结尾太长.如果不在此代码中测试长度,则可以在结尾处滑动任何非换行符.这是从其他答案修改的代码.
import re
r = re.compile('././.{4} .{2}:.{2}')
s = 'x/x/xxxx xx:xx'
if len(s) == 14:
if r.match(s):
print 'matches'
Run Code Online (Sandbox Code Playgroud)