如何在批处理文件中找到所有未终止的百分号实例?

huangapple go评论104阅读模式
英文:

How to find all instances of an unterminated percent sign in a batch file?

问题

这是您要翻译的代码部分:

def check_unterminated_percent_signs(content):
    # check for unterminated percent signs
    errors = []
    for i, line in enumerate(content.split('\n')):
        match = re.search(r'%[^%\n]*$', line)
        if match:
            errors.append(f'Unterminated percent sign found on line {i+1}, position {match.start()}: {line.strip()}')
    return errors

以下是已经翻译好的内容:

我有一个Python函数,用于查找批处理文件中的潜在错误,我正在尝试解决的问题之一是查找任何具有未终止百分号的行。然而,此函数似乎返回了每个具有百分号的行上的每个“%”符号。

目前的函数如下,但它没有完成我需要的任务:

以下是不应触发错误的批处理文件中的一行:

set Log_File=%Backup_Folder%\Logs\daily_copy.log 
英文:

I have a python function that looks through a batch file for potential errors, and the one that I am looking to resolve is to find any lines that have an unterminated percent sign. However, this function seems to be returning every % sign on every line that has a % sign.

Here is the function I have at the moment, which is not doing what I need it to do:

def check_unterminated_percent_signs(content):
    # check for unterminated percent signs
    errors = []
    for i, line in enumerate(content.split('\n')):
        match = re.search(r'%[^%\n]*$', line)
        if match:
            errors.append(f'Unterminated percent sign found on line {i+1}, position {match.start()}: {line.strip()}')
    return errors

<br>

Here is a line from the batch file that should not trigger the error:

set Log_File=%Backup_Folder%\Logs\daily_copy.log 

答案1

得分: 0

这是解决我的问题的代码:

def check_unterminated_percent_signs(content):
    # 检查未终止的百分号符号
    errors = []
    for i, line in enumerate(content.split('\n')):
        num_percent_signs = line.count('%')
        if num_percent_signs % 2 == 1:
            errors.append(f'第{i+1}行发现未终止的百分号符号: {line.strip()}')
    return errors
英文:

Thanks DarkKnight,

This is the code that solved my problem:

def check_unterminated_percent_signs(content):
    # check for unterminated percent signs
    errors = []
    for i, line in enumerate(content.split(&#39;\n&#39;)):
        num_percent_signs = line.count(&#39;%&#39;)
        if num_percent_signs % 2 == 1:
            errors.append(f&#39;Unterminated percent sign found on line {i+1}: {line.strip()}&#39;)
    return errors

huangapple
  • 本文由 发表于 2023年3月31日 18:32:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定