-
-
Save tazjel/2861976 to your computer and use it in GitHub Desktop.
Python Regex Question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Traceback (most recent call last): | |
File "/usr/local/bin/../Cellar/python3/3.2.2/lib/python3.2/functools.py", line 176, in wrapper | |
result = cache[key] | |
KeyError: (<class 'str'>, '(^.*def\\W*)(\\w+)\\W*\\((.*)?\\):', 0) | |
During handling of the above exception, another exception occurred: | |
Traceback (most recent call last): | |
File "./pickout_fn_defs.py", line 25, in <module> | |
FnDefRe = re.compile(r'(^.*def\W*)(\w+)\W*\((.*)?\):') | |
File "/usr/local/bin/../Cellar/python3/3.2.2/lib/python3.2/re.py", line 206, in compile | |
return _compile(pattern, flags) | |
File "/usr/local/bin/../Cellar/python3/3.2.2/lib/python3.2/re.py", line 255, in _compile | |
return _compile_typed(type(pattern), pattern, flags) | |
File "/usr/local/bin/../Cellar/python3/3.2.2/lib/python3.2/functools.py", line 180, in wrapper | |
result = user_function(*args, **kwds) | |
File "/usr/local/bin/../Cellar/python3/3.2.2/lib/python3.2/re.py", line 267, in _compile_typed | |
return sre_compile.compile(pattern, flags) | |
File "/usr/local/bin/../Cellar/python3/3.2.2/lib/python3.2/sre_compile.py", line 495, in compile | |
code = _code(p, flags) | |
File "/usr/local/bin/../Cellar/python3/3.2.2/lib/python3.2/sre_compile.py", line 480, in _code | |
_compile(code, p.data, flags) | |
File "/usr/local/bin/../Cellar/python3/3.2.2/lib/python3.2/sre_compile.py", line 74, in _compile | |
elif _simple(av) and op is not REPEAT: | |
File "/usr/local/bin/../Cellar/python3/3.2.2/lib/python3.2/sre_compile.py", line 359, in _simple | |
raise error("nothing to repeat") | |
sre_constants.error: nothing to repeat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import os.path | |
import sys | |
import re | |
def read_filename(): | |
while True: | |
filename = input('Enter filename: ' ) | |
if os.path.exists(filename): | |
print('Found file ' + filename) | |
break | |
print('Did not find file ' + filename + '. Try again') | |
return filename | |
def printFnName(mLine): | |
print("Function: ", mLine.group(2)) | |
def printArgs(mLine): | |
for arg in re.split(',', mLine.group(3)): | |
print("Arg: ", arg) | |
FnDefRe = re.compile(r'(^.*def\W*)(\w+)\W*\((.*)?\):') | |
# group-1 is 'def', group-2 is the fn name, group-3 are the args | |
# | |
def matchDefLine(line): | |
return FnDefRe.match(line) | |
def process_all_lines(filename): | |
f = open(filename) | |
i = 1 | |
lineLst = f.read().split('\n') | |
for line in lineLst: | |
#print(line) | |
mLine = matchDefLine(line) | |
if mLine: | |
print('---') | |
printFnName(mLine) | |
printArgs(mLine) | |
#-- | |
i = i + 1 | |
f.close() | |
def main(): | |
filename = read_filename() | |
print ('Filename = ' + filename) | |
print ('Processing file ' + filename) | |
process_all_lines(filename) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This one works, it was the regex as it was given to us | |
FnDefRe = re.compile(r'(^.*def\W*)(\w+)\W*\((.*)\):') | |
# This one does not work, throws an exception | |
FnDefRe = re.compile(r'(^.*def\W*)(\w+)\W*\((.*)?\):') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment