Browse Source

Some improvment

master
Rémi BERTHO 4 years ago
parent
commit
dfd1015e8c
Signed by: dalan GPG Key ID: EE3B917931C07B64
  1. 28
      SSnR.py

28
SSnR.py

@ -35,19 +35,37 @@ def main():
parser.add_argument('-s', '--string', help='String', required=True)
args = vars(parser.parse_args())
search(args["regex"], args["string"])
# Compile regex
try:
ex = compile_regex(args["regex"])
except SyntaxError as exception:
print("Error when compiling regex: " + str(exception))
return -1
except regex.error as exception:
print("Error when compiling regex: " + exception.msg)
return -1
search(ex, args["string"])
return 0
def compile_regex(ex):
"""
Compile regex
:param ex: Regular expression
"""
regex_compile = regex.compile(ex, regex.MULTILINE)
if regex_compile is None:
raise SyntaxError('Error in the regex')
else:
return regex_compile
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)
ite = ex.finditer(string)
nb_match = 0
for match in ite:
nb_match += 1

Loading…
Cancel
Save