SSnR/SSnR.py

61 lines
1.7 KiB
Python
Raw Normal View History

2017-10-08 16:12:27 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# SSnR.py
#
# Copyright 2017 Rémi BERTHO <remi.bertho@dalan.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import sys
import argparse
2017-10-08 16:12:27 +00:00
import regex
def main():
2017-10-08 16:12:27 +00:00
"""
Main function
"""
parser = argparse.ArgumentParser(description='Search and replace tool', prog='SSnR')
parser.add_argument('-r', '--regex', help='Regex', required=True)
parser.add_argument('-s', '--string', help='String', required=True)
args = vars(parser.parse_args())
2017-10-08 16:12:27 +00:00
search(args["regex"], args["string"])
2017-10-08 16:12:27 +00:00
return 0
def search(ex, string):
"""
Search in a string
:param ex: Regular expression
:param string: A string
2017-10-08 16:12:27 +00:00
"""
regex_compile = regex.compile(ex, regex.MULTILINE)
if regex_compile is None:
return -1
ite = regex_compile.finditer(string)
nb_match = 0
for match in ite:
nb_match += 1
print(" - Found \"" + match.group(0) + "\" at [" + str(match.start(0)) +
":" + str(match.end(0)) + "]")
print("Number of match: " + str(nb_match))
if __name__ == '__main__':
sys.exit(main())