Browse Source

Add replace

Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
master
Rémi BERTHO 4 years ago
parent
commit
51b4b0f5ee
Signed by: dalan GPG Key ID: EE3B917931C07B64
  1. 18
      SSnR.py

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

Loading…
Cancel
Save