Some improvment
This commit is contained in:
parent
2abb74f651
commit
dfd1015e8c
1 changed files with 23 additions and 5 deletions
28
SSnR.py
28
SSnR.py
|
@ -35,19 +35,37 @@ def main():
|
||||||
parser.add_argument('-s', '--string', help='String', required=True)
|
parser.add_argument('-s', '--string', help='String', required=True)
|
||||||
args = vars(parser.parse_args())
|
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
|
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):
|
def search(ex, string):
|
||||||
"""
|
"""
|
||||||
Search in a string
|
Search in a string
|
||||||
:param ex: Regular expression
|
:param ex: Regular expression
|
||||||
:param string: A string
|
:param string: A string
|
||||||
"""
|
"""
|
||||||
regex_compile = regex.compile(ex, regex.MULTILINE)
|
ite = ex.finditer(string)
|
||||||
if regex_compile is None:
|
|
||||||
return -1
|
|
||||||
ite = regex_compile.finditer(string)
|
|
||||||
nb_match = 0
|
nb_match = 0
|
||||||
for match in ite:
|
for match in ite:
|
||||||
nb_match += 1
|
nb_match += 1
|
||||||
|
|
Loading…
Reference in a new issue