Improve replace
Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
This commit is contained in:
parent
51b4b0f5ee
commit
b46281ffb8
1 changed files with 7 additions and 3 deletions
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…
Reference in a new issue