Reputation: 961
I would like to match a function definition in a LISP-like code. The function can be defined in either 4 formats
1. procedure(functionName(argList) mainCode)
2. (procedure functionName (argList) mainCode)
3. defun(functionName(argList) mainCode)
4. (defun functionName (argList) mainCode)
I am using the code as below to match the above possibilities using the re.search. This works fine. However when I capture the match using match.group(1) then this only works if the match happened on the first pattern.
Question: How to capture all 4 possibilities?
#!/usr/bin/python -tt
import os
import re # Import regular expression module
import sys
# traverse root directory, and list directories as dirs and files as files
def GetFunctionsFromSkillFile(skillFile):
with open(skillFile) as f:
data = f.readlines()
for line in data:
# Match Procedure
match=re.search(r'^\s*procedure\(\s*(\w+)|^\s*\(\s*procedure\s+(\w+)|^\s*defun\(\s*(\w+)|^\s*\(\s*defun\s+(\w+)', line)
if match:
# Capture the match
print('{0}.autoload="{1}"'.format(match.group(1), os.path.basename(skillFile)))
# Python boiler plate call.
if __name__ == "__main__":
# Quick check on arguments
if len (sys.argv) < 2:
print ('Usage: CreateAutoloadsFile.py skillDirectory')
sys.exit(2)
# Get the Excel file from cmd line arguments
skillDir = sys.argv[1]
# Traverse
for root, dirs, files in os.walk(skillDir):
for file in sorted(files):
if re.search(r'.il$', file):
#print(file)
file = root + "/" + file
GetFunctionsFromSkillFile(file)
Upvotes: 0
Views: 101
Reputation: 56809
You can rewrite your regex as:
r'^\s*(?:[(]\s*(?:defun|procedure)\s+|(?:defun|procedure)\s*[(]\s*)(\w+)'
Then regardless of the format, the name can be found in group 1.
Upvotes: 1