Some improvment

This commit is contained in:
Rémi BERTHO 2017-10-12 20:54:29 +02:00
parent 2abb74f651
commit dfd1015e8c
Signed by: dalan
GPG Key ID: EE3B917931C07B64
1 changed files with 23 additions and 5 deletions

28
SSnR.py
View File

@ -35,19 +35,37 @@ def main():
parser.add_argument('-s', '--string', help='String', required=True)
args = vars(parser.parse_args())
search(args["regex"], args["string"])
# Compile regex
try:
ex = compile_regex(args["regex"])
except SyntaxError as exception:
print("Error when compiling regex: " + str(exception))
return -1
except regex.error as exception:
print("Error when compiling regex: " + exception.msg)
return -1
search(ex, args["string"])
return 0
def compile_regex(ex):
"""
Compile regex
:param ex: Regular expression
"""
regex_compile = regex.compile(ex, regex.MULTILINE)
if regex_compile is None:
raise SyntaxError('Error in the regex')
else:
return regex_compile
def search(ex, string):
"""
Search in a string
:param ex: Regular expression
:param string: A string
"""
regex_compile = regex.compile(ex, regex.MULTILINE)
if regex_compile is None:
return -1
ite = regex_compile.finditer(string)
ite = ex.finditer(string)
nb_match = 0
for match in ite:
nb_match += 1