Add replace
Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
This commit is contained in:
parent
dfd1015e8c
commit
51b4b0f5ee
1 changed files with 16 additions and 2 deletions
16
SSnR.py
16
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,6 +46,9 @@ def main():
|
|||
print("Error when compiling regex: " + exception.msg)
|
||||
return -1
|
||||
|
||||
if args["replace"] is not None:
|
||||
replace(ex, args["string"], args["replace"])
|
||||
else:
|
||||
search(ex, args["string"])
|
||||
return 0
|
||||
|
||||
|
@ -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…
Reference in a new issue