#!/usr/bin/env python # -*- coding: utf-8 -*- # # SSnR.py # # Copyright 2017 RĂ©mi BERTHO # # 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 import regex 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('-s', '--string', help='String', required=True) args = vars(parser.parse_args()) search(args["regex"], args["string"]) return 0 def search(ex, string): """ Search in a string :param ex: Regular expression :param string: A string """ 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())