无法在 python 中更改大小写
can not change case in python
在下面的代码中,我想根据 openningline
的大小写更改 End
的大小写。
但是,它不起作用,即无论 openningline
的情况如何,End
总是 End
,而不是 end
或 END
。
我这里做错了什么?
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}' ),
]
您似乎希望 openningline
成为 re
。因此,当您使用 self.openningline.pattern.istitle()
时,您测试的是正则表达式模式,而不是您尝试与 re
匹配的行。大多数情况下,istitle()
、islower()
和 isupper()
.
的正则表达式模式将为 False
我不能完全理解你想要做什么,但也许你应该使用 line.istitle()
等等。
您没有使用正则表达式来匹配任何内容。应该是:
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()
在下面的代码中,我想根据 openningline
的大小写更改 End
的大小写。
但是,它不起作用,即无论 openningline
的情况如何,End
总是 End
,而不是 end
或 END
。
我这里做错了什么?
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}' ),
]
您似乎希望 openningline
成为 re
。因此,当您使用 self.openningline.pattern.istitle()
时,您测试的是正则表达式模式,而不是您尝试与 re
匹配的行。大多数情况下,istitle()
、islower()
和 isupper()
.
False
我不能完全理解你想要做什么,但也许你应该使用 line.istitle()
等等。
您没有使用正则表达式来匹配任何内容。应该是:
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()