I'm doing something like "Syntax Analyzer" with Kivy
, using re
(regular expresions).
I only want to check a valid syntax for basic operations (like +|-|*|/|(|)). The user tape the string (with keyboard) and I validate it with regex. But I don't know how to use regex in an if statement. That I want is: If the string that user brings me isn't correct (or doesn't check with regex) print something like "inavlid string" and if is correct print "Valid string".
I've tried with:
if re.match(patron, string) is not None:
print ("\nTrue")
else:
print("False")
but, it doesn't matter what do string
has, the app always show True
.
Sorry my poor english. Any help would be greatly appreciated!
import re
patron= re.compile(r"""
(
-?\d+[.\d+]?
[+*-/]
-?\d+[.\d+]?
[+|-|*|/]?
)*
""", re.X)
obj1= self.ids['text'].text #TextInput
if re.match(patron, obj1) is not None:
print ("\nValid String")
else:
print("Inavlid string")
if obj1= "53.22+22.11+10*555+62+55.2-66"
actually it's correct and app prints "Valid..." but if I put an a
like this "a53.22+22.11+10*555+62+55.2-66"
it's incorrect and the app must prints invalid..
but instead it still valid
.