gpt4 book ai didi

python - 在 python 中使用 re.verbose 时使用三个开闭引号的原因

转载 作者:行者123 更新时间:2023-11-28 22:04:56 34 4
gpt4 key购买 nike

我很好奇为什么我看到的大多数示例在使用正则表达式时使用 re.verbose,而使用 '''?

下面是我找到的一个例子

address = re.compile(
''' #THIS
[\w\d.+-]+ # username
@
([\w\d.]+\.)+ # domain name prefix
(com|org|edu) # we should support more top-level domains
''', #AND THIS
re.UNICODE | re.VERBOSE)

最佳答案

使用 ''' 允许字符串跨越多行,因此它们可以分解 re 的每个部分,而不是有一个长行。

这样做是为了代码的可读性,而不是功能。

参见 Python Tutorial strings section有关跨多行的字符串的更多信息。

关于python - 在 python 中使用 re.verbose 时使用三个开闭引号的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6012806/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com