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.matchstarts from the beginning and returns an object matchif the string is correct, or Noneotherwise. The result boolwill 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