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

问题描述

最近,我一直在用 Python 3.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

请帮帮我!

感谢任何帮助! :)

解决方法

我不知道正则表达式是否适合这里。您可以只使用作为标准库一部分的 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 语句更合适,但您明白了。