Reputation: 3218
in the code below, I want to change the case of End
, depending on the case of openningline
.
But, it is not working, i.e. nomatter the case of openningline
, End
is always End
, not end
or END
.
What I am doing wrong here?
class SyntaxElement:
def __init__(self, openningline, closingline):
self.openningline = openningline
self.closingline = closingline
def match(self, line):
""" Return (indent, closingline) or (None, None)"""
match = self.openningline.search(line)
if match:
indentpattern = re.compile(r'^\s*')
variablepattern = re.compile(r'\$\{(?P<varname>[a-zA-Z0-9_]*)\}')
indent = indentpattern.search(line).group(0)
if self.openningline.pattern.istitle():
closingline = self.closingline.title()
elif self.openningline.pattern.islower():
closingline = self.closingline.lower()
elif self.openningline.pattern.isupper():
closingline = self.closingline.upper()
else:
closingline = self.closingline
# expand variables in closingline
while True:
variable_match = variablepattern.search(closingline)
if variable_match:
try:
replacement = match.group(variable_match.group('varname'))
except:
print("Group %s is not defined in pattern" % variable_match.group('varname'))
replacement = variable_match.group('varname')
try:
closingline = closingline.replace(variable_match.group(0), replacement)
except TypeError:
if replacement is None:
replacement = ""
closingline = closingline.replace(variable_match.group(0), str(replacement))
else:
break
else:
return (None, None)
closingline = closingline.rstrip()
return (indent, closingline)
def fortran_complete():
syntax_elements = [
SyntaxElement(re.compile(r'^\s*\s*((?P<struc>([A-z0-9]*)))\s*((?P<name>([a-zA-Z0-9_]+)))', re.IGNORECASE),
'End ${struc} ${name}' ),
]
Upvotes: 1
Views: 74
Reputation: 780974
You're not using the regular expression to match anything. It should be:
matched = self.openningline.search(line).group(0)
if matched.istitle():
closingline = closingline.title()
elif matched.isupper():
closingline = closingline.upper()
elif matched.islower():
closingline = closingline.lower()
Upvotes: 2
Reputation: 94
It seems you expect openningline
to be a re
. So, when you use self.openningline.pattern.istitle()
you are testing the regular expression pattern not the line you are trying to match with the re
. Most of the time the regular expressions patterns will be False
for istitle()
, islower()
and isupper()
.
I am not exactly able to understand what you are trying to do but maybe you should use line.istitle()
and so on.
Upvotes: 1