【问题标题】:can not change case in python不能在python中改变大小写
【发布时间】:2020-07-07 03:35:20
【问题描述】:

在下面的代码中,我想根据openningline的大小写改变End的大小写。 但是,它不起作用,即无论openninglineEnd 的情况始终是End,而不是endEND

我在这里做错了什么?

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&lt;struc&gt;([A-z0-9]*)))\s*((?P&lt;name&gt;([a-zA-Z0-9_]+))) 的模式,它具有复杂的大小写混合。你的意思是它匹配的字符串的大小写吗?
  • 顺便说一句,A-z 是错误的。使用A-Za-z——你不需要同时使用大写和小写,因为你使用的是re.IGNORECASE
  • @Barmar:你的第一条评论是。对于第二个,你是对的。但这并没有改变任何事情。

标签: python case python-re


【解决方案1】:

您没有使用正则表达式来匹配任何内容。应该是:

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()

【讨论】:

  • 为了简洁起见,习惯上将匹配对象称为m,这样可以使这段代码更清晰易读。或者也许mo 用于“比赛开场线”。 (在此代码上,我将重命名 closingline clopenningline ol
  • 顺便说一句,有一点值得评论:您的代码静默处理(非常罕见的)第四种情况,即开头的文本是混合大小写,但既不是标题大小写,也不是全大写,也不是全低(例如beGiN)。在这种情况下,它什么也不做,保持原样。
【解决方案2】:

您似乎希望openninglinere。因此,当您使用self.openningline.pattern.istitle() 时,您测试的是正则表达式模式,而不是您尝试与re 匹配的行。大多数情况下,对于istitle()islower()isupper(),正则表达式模式将是False

我不能完全理解你想要做什么,但也许你应该使用line.istitle() 等等。

【讨论】:

  • 我没明白你的意思。 re.compile("Hello").pattern.istitle() True
猜你喜欢
  • 2014-11-04
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多