在Python中使用正则表达式提取文本

在Python中使用正则表达式提取文本,python,regex,Python,Regex,如何使用regex(或其他衍生工具)从以下可能的排列中提取roundUp(…): [[[ roundUp( 10.0 ) ]]] [[[ roundUp( 10.0 + 2.0 ) ]]] [[[ roundUp( (10.0 * 2.0) + 2.0 ) ]]] [[[ 10.0 + roundUp( (10.0 * 2.0) + 2.0 ) ]]] [[[ 10.0 + roundUp( (10.0 * 2.0) + 2.0 ) + 20.0 ]]] 我问的原因是我想在我的代码中将roun

如何使用
regex
(或其他衍生工具)从以下可能的排列中提取
roundUp(…)

[[[ roundUp( 10.0 ) ]]]
[[[ roundUp( 10.0 + 2.0 ) ]]]
[[[ roundUp( (10.0 * 2.0) + 2.0 ) ]]]
[[[ 10.0 + roundUp( (10.0 * 2.0) + 2.0 ) ]]]
[[[ 10.0 + roundUp( (10.0 * 2.0) + 2.0 ) + 20.0 ]]]

我问的原因是我想在我的代码中将
roundUp(…)
替换为
math.ceil(…)*100)/100.0
,但我不知道该怎么做,因为偶然括号被多次用于帮助操作符优先级

这是python,为什么不重新绑定名称
roundUp

def my_roundup(x):
  return math.ceil(x*100)/100.

roundUp = my_roundup

这是python,为什么不重新绑定名称

def my_roundup(x):
  return math.ceil(x*100)/100.

roundUp = my_roundup

正则表达式无法解决一般情况。正则表达式的功能不足以表示任何类似于堆栈的内容,例如括号或嵌套到任意深度的XML标记

如果您正在用python解决这个问题,您可以执行以下操作

import re

def roundup_sub(m):
    close_paren_index = None
    level = 1
    for i, c in enumerate(m.group(1)):
        if c == ')':
            level -= 1
        if level == 0:
            close_paren_index = i
            break
        if c == '(':
            level += 1
    if close_paren_index is None:
        raise ValueError("Unclosed roundUp()")
    return 'math.ceil((' + m.group(1)[1:close_paren_index] + ')*100)/100.0' + \
            m.group(1)[close_paren_index:]    # matching ')' and everything after

def replace_every_roundup(text):
    while True:
        new_text = re.sub(r'(?ms)roundUp\((.*)', roundup_sub, text)
        if new_text == text:
            return text
        text = new_text
这将使用re.sub的repl=函数形式,并使用正则表达式查找开头,使用python匹配括号并决定在何处结束替换


使用它们的示例:

my_text = """[[[ roundUp( 10.0 ) ]]]
[[[ roundUp( 10.0 + 2.0 ) ]]]
[[[ roundUp( (10.0 * 2.0) + 2.0 ) ]]]
[[[ 10.0 + roundUp( (10.0 * 2.0) + 2.0 ) ]]]
[[[ 10.0 + roundUp( (10.0 * 2.0) + 2.0 ) + 20.0 ]]]"""
print replace_every_roundup(my_text)
这将为您提供输出

[[[ math.ceil((10.0 )*100)/100.0) ]]]
[[[ math.ceil((10.0 + 2.0 )*100)/100.0) ]]]
[[[ math.ceil(((10.0 * 2.0) + 2.0 )*100)/100.0) ]]]
[[[ 10.0 + math.ceil(((10.0 * 2.0) + 2.0 )*100)/100.0) ]]]
[[[ 10.0 + math.ceil(((10.0 * 2.0) + 2.0 )*100)/100.0) + 20.0 ]]]


另一种选择是实现一个正则表达式,该正则表达式最多可以处理一定深度的嵌套括号。

正则表达式无法解决一般情况。正则表达式的功能不足以表示任何类似于堆栈的内容,例如括号或嵌套到任意深度的XML标记

如果您正在用python解决这个问题,您可以执行以下操作

import re

def roundup_sub(m):
    close_paren_index = None
    level = 1
    for i, c in enumerate(m.group(1)):
        if c == ')':
            level -= 1
        if level == 0:
            close_paren_index = i
            break
        if c == '(':
            level += 1
    if close_paren_index is None:
        raise ValueError("Unclosed roundUp()")
    return 'math.ceil((' + m.group(1)[1:close_paren_index] + ')*100)/100.0' + \
            m.group(1)[close_paren_index:]    # matching ')' and everything after

def replace_every_roundup(text):
    while True:
        new_text = re.sub(r'(?ms)roundUp\((.*)', roundup_sub, text)
        if new_text == text:
            return text
        text = new_text
这将使用re.sub的repl=函数形式,并使用正则表达式查找开头,使用python匹配括号并决定在何处结束替换


使用它们的示例:

my_text = """[[[ roundUp( 10.0 ) ]]]
[[[ roundUp( 10.0 + 2.0 ) ]]]
[[[ roundUp( (10.0 * 2.0) + 2.0 ) ]]]
[[[ 10.0 + roundUp( (10.0 * 2.0) + 2.0 ) ]]]
[[[ 10.0 + roundUp( (10.0 * 2.0) + 2.0 ) + 20.0 ]]]"""
print replace_every_roundup(my_text)
这将为您提供输出

[[[ math.ceil((10.0 )*100)/100.0) ]]]
[[[ math.ceil((10.0 + 2.0 )*100)/100.0) ]]]
[[[ math.ceil(((10.0 * 2.0) + 2.0 )*100)/100.0) ]]]
[[[ 10.0 + math.ceil(((10.0 * 2.0) + 2.0 )*100)/100.0) ]]]
[[[ 10.0 + math.ceil(((10.0 * 2.0) + 2.0 )*100)/100.0) + 20.0 ]]]


另一个选择是实现一个正则表达式,该正则表达式可以处理一定深度的嵌套括号。

为什么我没有想到这一点。谢谢你的简单解决方案贪婪的正则表达式行不通,因为排列是无限的我怎么没想到呢。谢谢你的简单解决方案贪婪的正则表达式行不通,因为排列是无限的你能给我一个使用这些函数的简单例子吗?你能给我一个使用这些函数的简单例子吗?