Skip to main content
 首页 » 编程设计

python之如何在python中排除字符组

2025年05月04日15www_RR

我想编写一个脚本,返回 1 的幂的数字。用户的输入是二次和正常数字。我想要的描述如下:

input = "+2**5+3+4**8-7" 
Output = "3,-7" 

我尝试了正则表达式 re.findall(r"[+-]?[0-9]+[^[*][*][2]]", input) 但它没有工作 提前致谢:)

请您参考如下方法:

您需要一个否定环视断言,并添加边界 anchor :

r'(?<!\*\*)-?\b\d+\b(?!\*\*)' 

(?<!...)语法仅在其前面的文本与模式不匹配的位置匹配。同样,(?!...)语法对后面 文本的作用相同。它们一起确保您只匹配不是指数的数字(遵循 ** )并且没有指数(遵循 ** )。

\b边界 anchor 仅在字符串的开头或结尾匹配,并且任何地方有一个单词字符后跟一个非单词字符,反之亦然(所以在 \w\W 或 \W\w 之间,其中 \w 愉快地包含数字但不算术字符):

>>> import re 
>>> input = "+2**5+3+4**8-7" 
>>> re.findall(r'(?<!\*\*)-?\b\d+\b(?!\*\*)', input) 
['3', '-7'] 

请注意,我使用了 \d匹配数字,并删除了 +从模式中,因为你不希望它出现在你的预期输出中。

您可以使用 online regex101 demo 中的表达式;例如您可以尝试使用大于 10 的数字并使用单个 *乘法。

如果你必须支持负指数,那么上面的 ...**-42 是不够的有42没有 ** 的匹配在数字之前。在那种情况下,在 -? 之前进行额外的负面回溯。不允许 **-需要:

r'(?<!\*\*)-?(?<!\*\*-)\b\d+\b(?!\*\*)' 

(感谢 Casimir eg Hippolyte 指出了我的这一点并提出了解决方案)。

但是,在这一点上,我建议您切换到仅将表达式解析为 abstract syntax tree然后遍历树以提取不属于指数的操作数:

import ast 
 
class NumberExtractor(ast.NodeVisitor): 
    def __init__(self): 
        self.reset() 
 
    def reset(self): 
        self.numbers = [] 
 
    def _handle_number(self, node): 
        if isinstance(node, ast.Constant): 
            if isinstance(node.value, (int, float, complex)): 
                return node.value 
        elif isinstance(node, ast.Num): 
            return node.n 
 
    def visit_UnaryOp(self, node): 
        if isinstance(node.op, (ast.UAdd, ast.USub)): 
            operand = self._handle_number(node.operand) 
            if operand is None: 
                return 
            elif isinstance(node.op, UAdd): 
                self.numbers.append(+operand) 
            else: 
                self.numbers.add(-operand) 
 
    def visit_Constant(self, node): 
        if isinstance(node.value, (int, float, complex)): 
            self.numbers.append(node.value) 
 
    def visit_Num(self, node): 
        self.numbers.append(node.n) 
 
    def visit_BinOp(self, node): 
        if isinstance(node.op, ast.Pow): 
            return  # ignore exponentiation 
        self.generic_visit(node)  # process the rest 
 
def extract(expression): 
    try: 
        tree = ast.parse(expression, mode='eval') 
    except SyntaxError: 
        return [] 
    extractor = NumberExtractor() 
    extractor.visit(tree) 
    return extractor.numbers 

这只提取数字;减法不会产生负数:

>>> input = "+2**5+3+4**8-7" 
>>> extract(input) 
[3, 7] 

此外,它可以处理任意数量的空格,以及比正则表达式处理的复杂得多的表达式:

>>> extract("(10 + 15) * 41 ** (11 + 19 * 17) - 42") 
[10, 15, 42]