Add replace

Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
This commit is contained in:
Rémi BERTHO 2017-10-13 18:36:54 +02:00
parent dfd1015e8c
commit 51b4b0f5ee
Signed by: dalan
GPG Key ID: EE3B917931C07B64
1 changed files with 16 additions and 2 deletions

18
SSnR.py
View File

@ -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())