diff --git a/SSnR.py b/SSnR.py index 202c31e..2350b28 100644 --- a/SSnR.py +++ b/SSnR.py @@ -34,6 +34,8 @@ def main(): 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) + parser.add_argument('-m', '--print_nb_match', help='Print the number of match in replace', + required=False, action='store_true') args = vars(parser.parse_args()) # Compile regex @@ -47,7 +49,7 @@ def main(): return -1 if args["replace"] is not None: - replace(ex, args["string"], args["replace"]) + replace(ex, args["string"], args["replace"], args["print_nb_match"]) else: search(ex, args["string"]) return 0 @@ -78,15 +80,17 @@ def search(ex, string): print("Number of match: " + str(nb_match)) -def replace(ex, string, replace_string): +def replace(ex, string, replace_string, print_nb): """ Replace in a string :param ex: Regular expression :param string: A string + :param print_nb: Print the number of match """ res = ex.subn(replace_string, string) print(res[0]) - print("Number of match: " + str(res[1])) + if print_nb: + print("Number of match: " + str(res[1])) if __name__ == '__main__': sys.exit(main())