【发布时间】:2020-07-07 03:35:20
【问题描述】:
在下面的代码中,我想根据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}' ),
]
【问题讨论】:
-
您能否添加示例输入、用法和所需输出
-
其实这是一个更大的代码的一部分,一个vim插件。所以,
mwe是不可能的。但是,从测试中不是很清楚吗? -
“openningline 的案例”是什么意思?
openningline是一个类似于^\s*\s*((?P<struc>([A-z0-9]*)))\s*((?P<name>([a-zA-Z0-9_]+)))的模式,它具有复杂的大小写混合。你的意思是它匹配的字符串的大小写吗? -
顺便说一句,
A-z是错误的。使用A-Z或a-z——你不需要同时使用大写和小写,因为你使用的是re.IGNORECASE。 -
@Barmar:你的第一条评论是。对于第二个,你是对的。但这并没有改变任何事情。