>>> dot = re.compile("[^.]*\.[^.]*$")
>>> dot.match("fooooooooooooo.bar")
<_sre.SRE_Match object at 0xb7651838>
>>> dot.match("fooooooooooooo.bar.sad") is None
True
>>>
编辑:
如果只考虑整数和小数,就更简单了:
def valid(s):
return re.match('[0-9]+(\.[0-9]*)?$', s) is not None
assert valid("42")
assert valid("13.37")
assert valid("1.")
assert not valid("1.2.3.4")
assert not valid("abcd")
Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.
一定要
match
,不要search
编辑:
如果只考虑整数和小数,就更简单了:
您可以使用:
(匹配周期0或1次。)
不需要regexp,请参见^{} :
相关问题 更多 >
编程相关推荐