繁体   English   中英

特定格式的字符串

[英]String in specific format

如果字符串格式正确,我如何检查python?
我有变量标签,它应该是0.0格式

例如

tag = "1.0"

if tag != (is not in format **0.0**):
    print("not in tag")"
else:
    print("correct tag")"

在 bash 中它看起来像这样

tag="10.0"
if [[ $tag != "${tag%.*}.${tag#*.}" ]]; then
    echo "wrong tag"
fi

最简单的实现是使用正则表达式:

if not re.search("^\d+\.\d+$", tag):
    #does not match

正如这篇文章所引用,我建议研究正则表达式。

前任:

import re
r = re.compile("^(\d*\.)?\d+$")
if r.match(tag) is not None:
   print('match')
else:
   print('not match')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM