If you want to check all input, I would suggest using re.match
.
>>> pattern = re.compile('(\(\d+,\d+\)\s*)+$')
>>> def isValid(string):
... return bool(pattern.match(string.strip()))
...
>>> string = '(1,1) (1,2) (1,3)'
>>> isValid(string)
True
Either the whole line matches, or nothing matches. By default, it re.match
starts from the beginning and returns an object match
if the string is correct, or None
otherwise. The result bool
will be used to evaluate the truth of this expression.
Note that the space character has been made optional to simplify the expression. if you want a strict match, I recommend looking at DYZ's answer .
Regular Expression Details
(
\(
\d+
,
\d+
\)
\s*
)+
$
source
share