Browse Source

Improve replace

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

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

Loading…
Cancel
Save