用括号逃脱的牙套格式化弦

发布于 2025-01-19 01:21:53 字数 1284 浏览 0 评论 0原文

我有一个字符串,其中包含牙套中的排列:

string = "3+3={3+3}"

我想找到所有的漏洞,并因表达式而替换它们(使用评估)。 我也想制作某种括号逃脱机制。 我可以自己做:

final = ""
opened_eval = False
short_escaping = False
long_escaping = False
inner_string = ""
for letter in string:
    if opened_eval:
        if letter == "}":
            final += eval(inner_string, kwargs)
            inner_string = ""
            opened_eval = False
        else:
            inner_string+=letter
    elif short_escaping:
        opened+=letter
        short_escaping=False
    elif long_escaping:
        if letter == "|":
            long_escaping=False
        else:
            opened+=letter
    else:
        if letter == "\\":
            short_escaping=True
        elif letter == "|":
            long_escaping=True
        elif letter=="{":
            opened_eval = True
        else:
            final+=letter
>>> print(final)
3+3=6

但是我关心它的工作速度,所以我想使用C函数。 我尝试使用re.finditer(r“ \ {\ w+\}”)来做到这一点,但是我不知道,我该如何逃脱re。所以我需要这样的东西:

for expression in find(r"a = {get_a()} \{text in braces\} b = {get_b()}"):
    print(expression.text, expression.start, expression.end)
{get_a()} 5 13
{get_b()} 38 46

I have a string, that contains expresions in braces:

string = "3+3={3+3}"

I want to find all expresions and replace them by result of expression (using eval).
Also I want to make some kind of braces escaping mechanism.
I can do it by myself:

final = ""
opened_eval = False
short_escaping = False
long_escaping = False
inner_string = ""
for letter in string:
    if opened_eval:
        if letter == "}":
            final += eval(inner_string, kwargs)
            inner_string = ""
            opened_eval = False
        else:
            inner_string+=letter
    elif short_escaping:
        opened+=letter
        short_escaping=False
    elif long_escaping:
        if letter == "|":
            long_escaping=False
        else:
            opened+=letter
    else:
        if letter == "\\":
            short_escaping=True
        elif letter == "|":
            long_escaping=True
        elif letter=="{":
            opened_eval = True
        else:
            final+=letter
>>> print(final)
3+3=6

But I care about, how quickly does it work, so I want to use C function.
I tryed to do that with re.finditer (r"\{\w+\}"), but I don't know, how can I make escaping with re. So I need something like this:

for expression in find(r"a = {get_a()} \{text in braces\} b = {get_b()}"):
    print(expression.text, expression.start, expression.end)
{get_a()} 5 13
{get_b()} 38 46

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2025-01-26 01:21:53

更快的方法:

import re之后,定义一个字符串:

string = "3+3={3+3}, 7:4={7/4}, etc.."

查找括号中的所有参数:

equations = re.findall("\{[^\{\}]*\}", string)
>>>['{3+3}', '{7/4}']

求解方程:

results = [list(eval(equation))[0] for equation in equations]
>>>[6, 1.75]

最后替换字符串中的括号(sub)并使用结果格式化字符串:

re.sub("\{[^\{\}]*\}","{}",string).format(*results)
>>>'3+3=6, 7:4=1.75, etc..'

Faster way to do this:

After import re, define a string:

string = "3+3={3+3}, 7:4={7/4}, etc.."

find all the arguments in the brackets:

equations = re.findall("\{[^\{\}]*\}", string)
>>>['{3+3}', '{7/4}']

solve the equations:

results = [list(eval(equation))[0] for equation in equations]
>>>[6, 1.75]

finally replace the brackets in the string (sub) and format the string with the results:

re.sub("\{[^\{\}]*\}","{}",string).format(*results)
>>>'3+3=6, 7:4=1.75, etc..'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文