不同的前缀正则表达式.编译()python中的参数

2024-09-25 04:29:57 发布

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

在写re.compile时,我多次使用r前缀(re.compile(r'(xyx))。但是,我第一次看到(re.compile(f'(xyx)),我不确定它在做什么。输出对我来说也没有任何意义。有人能解释一下这个f在这里做什么吗?在

import re, string
re_tok = re.compile(f'([{string.punctuation}“”¨«»®´·º½¾¿¡§£₤‘’])')
def tokenize(s): 
    return re_tok.sub(r' \1 ', s).split()


>>> tokenize('˚∆˙©∆©˙¬ ldgkl slgh lshsg ieh 954n bvery590oerfdb o3pg')

Tags: importrestringreturndef意义splitcompile
3条回答

格式化字符串文本或f-string是以“f”或“f”为前缀的字符串文本。在

这些字符串可能包含替换字段,这些字段是用大括号{}分隔的表达式。在

虽然其他字符串文本始终具有常量值,但格式化字符串实际上是在运行时计算的表达式。在

在这种情况下

'string.punctuation`` are a replacement field, i.e. the string is to be formatted with周围的大括号字符串.标点符号, which, in Python, is a 'string of ASCII characters which are considered punctuation marks in theC“语言环境”。在

要了解更多信息,请查看thesePython文档和^{} references:-)

这些是修改字符串文字行为的各种标志 r表示原始字符串,f表示字符串插值

参见政治公众人物的解释:

F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with 'f', which contains expressions inside braces. The expressions are replaced with their values. Some examples are:

>>> import datetime
>>> name = 'Fred'
>>> age = 50
>>> anniversary = datetime.date(1991, 10, 12)
>>> f'My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.'
'My name is Fred, my age next year is 51, my anniversary is Saturday, October 12, 1991.'
>>> f'He said his name is {name!r}.'
"He said his name is 'Fred'."

https://www.python.org/dev/peps/pep-0498/

以及python文档:

关于r

Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters. As a result, in string literals, '\U' and '\u' escapes in raw strings are not treated specially. Given that Python 2.x’s raw unicode literals behave differently than Python 3.x’s the 'ur' syntax is not supported.

https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

关于f

A formatted string literal or f-string is a string literal that is prefixed with 'f' or 'F'. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.

https://docs.python.org/3/reference/lexical_analysis.html#f-strings

根据python documentation

2.4.3. Formatted string literals

New in version 3.6.

A formatted string literal or f-string is a string literal that is prefixed with 'f' or 'F'. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.

文档中有多个示例,因此我将发布其中几个示例并解释:

name = "Fred"
f"He said his name is{name!r}."
# "He said his name is 'Fred'.

这里,!引入了一个转换字段。!r调用^{}

The result is then formatted using the format() protocol. The format specifier is passed to the __format__() method of the expression or conversion result. An empty string is passed when the format specifier is omitted. The formatted result is then included in the final value of the whole string.

由于它是使用format()协议格式化的,以下是其他用例:

^{pr2}$

偶数日期时间对象:

today = datetime(year=2017, month=1, day=27)
f"{today:%B %d, %Y}"
# January 27, 2017

根据上面的信息,我们将其应用到您的代码中:

f'([{string.punctuation}“”¨«»®´·º½¾¿¡§£₤‘’])'

上面的行将string.punctuation插入到该位置的字符串中。在

According to the docsstring.punctuation是:

String of ASCII characters which are considered punctuation characters in the C locale.

如果您真的想深入了解这个问题:什么是C语言环境?在

The C standard defines the locale as a program-wide property that may be relatively expensive to change. On top of that, some implementation are broken in such a way that frequent locale changes may cause core dumps. This makes the locale somewhat painful to use correctly.

Initially, when a program is started, the locale is the C locale, no matter what the user’s preferred locale is.

相关问题 更多 >