From 51b4b0f5ee8ee78ef235d624bca9cf8ab0e6ea25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20BERTHO?= Date: Fri, 13 Oct 2017 18:36:54 +0200 Subject: [PATCH] Add replace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: RĂ©mi BERTHO --- SSnR.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/SSnR.py b/SSnR.py index 7841180..202c31e 100644 --- a/SSnR.py +++ b/SSnR.py @@ -31,8 +31,9 @@ def main(): Main function """ parser = argparse.ArgumentParser(description='Search and replace tool', prog='SSnR') - parser.add_argument('-r', '--regex', help='Regex', required=True) + parser.add_argument('-e', '--regex', help='Regex', required=True) parser.add_argument('-s', '--string', help='String', required=True) + parser.add_argument('-r', '--replace', help='Replace', required=False) args = vars(parser.parse_args()) # Compile regex @@ -45,7 +46,10 @@ def main(): print("Error when compiling regex: " + exception.msg) return -1 - search(ex, args["string"]) + if args["replace"] is not None: + replace(ex, args["string"], args["replace"]) + else: + search(ex, args["string"]) return 0 def compile_regex(ex): @@ -74,5 +78,15 @@ def search(ex, string): print("Number of match: " + str(nb_match)) +def replace(ex, string, replace_string): + """ + Replace in a string + :param ex: Regular expression + :param string: A string + """ + res = ex.subn(replace_string, string) + print(res[0]) + print("Number of match: " + str(res[1])) + if __name__ == '__main__': sys.exit(main())