如何使用正则表达式排除Python F字符串中带括号的字符?

2024-10-02 08:29:49 发布

您现在位置:Python中文网/ 问答频道 /正文

最近,我在Python3.7.6中创建了一个编辑器(使用tkinter),我创建了以下语法来突出显示单引号、双引号和三引号,但我想排除f字符串的花括号内的所有字符,我尝试使用[^\{(.*)\}]作为一个否定集,但后来意识到它不起作用。我试着在网上搜索,但所有这些都不符合我的正则表达式

这是代码的正则表达式部分:

def regex_groups(self, name, alternates):
    return "(?P<%s>" % name + "|".join(alternates) + ")"

stringprefix = r"(\bB|b|br|Br|bR|BR|rb|rB|Rb|RB|r|u|R|U|f|F|fr|Fr|fR|FR|rf|rF|Rf|RF)?"
sqstring = stringprefix + r"'[^'\\\n]*(\\.[^'\\\n]*)*'?"
dqstring = stringprefix + r'"[^"\\\n]*(\\.[^"\\\n]*)*"?'
sqqqstring = stringprefix + r"'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?"
dqqqstring = stringprefix + r'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
string = self.regex_groups("STRING", [sqqqstring, dqqqstring, sqstring, dqstring])

我试着把stringprefix分成两个字符串r"(f|F|fr|Fr|fR|FR|rf|rF|Rf|RF)?"r"(B|b|br|Br|bR|BR|rb|rB|Rb|RB|r|u|R|U)?",然后分别与sqstring, dqstring, sq3string and dq3string一起使用,但没有成功

以下是正则表达式测试的一部分: enter image description here

请帮帮我

感谢您的帮助!:)


Tags: 字符串namebrselfregexgroupsrbalternates
1条回答
网友
1楼 · 发布于 2024-10-02 08:29:49

我不知道正则表达式是否适合这里。您可以使用tokenize模块(标准库的一部分)来解析Python源代码并对其进行标记。根据每个令牌的类型,您可以选择不同的颜色。例如:

import tokenize
from io import BytesIO

src = """
def foo(bar):
    print(bar, "hi there")
"""

tokens = tokenize.tokenize(BytesIO(src.encode("utf-8")).readline)

openers = ("class", "def", "for", "while", "if", "try", "except")

for token in tokens:
    color = ""
    line = token.start[0]
    start = token.start[1]
    end = token.end[1]
    if token.exact_type == tokenize.NAME and token.string in openers:
        color = "orange"
    elif token.exact_type == tokenize.NAME:
        color = "blue"
    elif token.exact_type == tokenize.STRING:
        color = "green"

    if color:
        print(f"token '{token.string}' (line: {line}, col: {start} - {end}) should be {color}")

输出:

token 'def' (line: 2, col: 0 - 3) should be orange
token 'foo' (line: 2, col: 4 - 7) should be blue
token 'bar' (line: 2, col: 8 - 11) should be blue
token 'print' (line: 3, col: 4 - 9) should be blue
token 'bar' (line: 3, col: 10 - 13) should be blue
token '"hi there"' (line: 3, col: 15 - 25) should be green
>>> 

一个将标记类型映射到颜色的查找表(字典)比一大块if语句更合适,但是你明白了

相关问题 更多 >

    热门问题