From dfd1015e8cdff47805c20e689339b09b4ef1f0e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20BERTHO?= Date: Thu, 12 Oct 2017 20:54:29 +0200 Subject: [PATCH] Some improvment --- SSnR.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/SSnR.py b/SSnR.py index 79319cf..7841180 100644 --- a/SSnR.py +++ b/SSnR.py @@ -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