#!/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 getopt import regex def main(args): """ Main function :param args: Main arguments """ try: opts, args = getopt.getopt(args, "hr:s:", ["regex=", "string=", "help"]) except getopt.GetoptError as err: print(err) print_help() return 2 string = "" ex = "" for opt, arg in opts: if opt in ("-h", "--help"): print_help() return 0 elif opt in ("-r", "--regex"): ex = arg elif opt in ("-s", "--string"): string = arg if (string == "") or (ex == ""): print_help() return -2 search(ex, 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)) def print_help(): print('SSnR.py -s -r ') if __name__ == '__main__': sys.exit(main(sys.argv[1:]))