热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

告诉原始字符串(r)和常规字符串(")?-Tellarawstring(r'')fromaregularstring('')?

Imcurrentlybuildingatoolthatwillhavetomatchfilenamesagainstapattern.Forconvenience,

I'm currently building a tool that will have to match filenames against a pattern. For convenience, I intend to provide both lazy matching (in a glob-like fashion) and regexp matching. For example, the following two snippets would eventually have the same effects:

我目前正在构建一个工具,它必须将文件名与模式匹配。为了方便起见,我打算提供两个延迟匹配(在全局样式中)和regexp匹配。例如,以下两个片段最终会产生相同的效果:

@mylib.rule('static/*.html')
def myfunc():
    pass

@mylib.rule(r'^static/([^/]+)\.html')
def myfunc():
    pass

AFAIK r'' is only useful to the Python parser and it actually creates a standard str instance after parsing (the only difference being that it keeps the \).

AFAIK r“只对Python解析器有用,它实际上在解析后创建了一个标准的str实例(惟一的区别是它保留了\)。

Is anybody aware of a way to tell one from another?

有没有人知道一种区分彼此的方法?

I would hate to have to provide two alternate decorators for the same purpose or, worse, resorting manually parsing the string to determine if it's a regexp or not.

我不希望为相同的目的提供两个替代decorator,或者更糟糕的是,通过手工解析字符串来确定它是否是regexp。

3 个解决方案

#1


13  

You can't tell them apart. Every raw string literal could also be written as a standard string literal (possibly requiring more quoting) and vice versa. Apart from this, I'd definitely give different names to the two decorators. They don't do the same things, they do different things.

你分不清他们。每个原始字符串文字也可以写成标准字符串文字(可能需要更多引用),反之亦然。除此之外,我肯定会给这两个decorator取不同的名字。他们不做同样的事,他们做不同的事。

Example (CPython):

例子(CPython的):

>>> a = r'^static/([^/]+)\.html'; b = '^static/([^/]+)\.html'
>>> a is b
True

So in this particular example, the raw string literal and the standard string literal even result in the same string object.

在这个例子中,原始字符串文字和标准字符串文字甚至会产生相同的字符串对象。

#2


11  

You can't tell whether a string was defined as a raw string after the fact. Personally, I would in fact use a separate decorator, but if you don't want to, you could use a named parameter (e.g. @rule(glob="*.txt") for globs and @rule(re=r".+\.txt") for regex).

您不能判断一个字符串是否被定义为在事实之后的原始字符串。实际上,我实际上是使用一个单独的decorator,但是如果您不想,您可以使用一个命名参数(例如@rule(glob="*.txt"),用于globs和@rule(re=r".+\.txt")。

Alternatively, require users to provide a compiled regular expression object if they want to use a regex, e.g. @rule(re.compile(r".+\.txt")) -- this is easy to detect because its type is different.

另外,如果用户希望使用regex,则需要用户提供一个已编译的正则表达式对象,例如@rule(re.compile(r".+\.txt")——这很容易检测,因为它的类型不同。

#3


1  

The term "raw string" is confusing because it sounds like it is a special type of string - when in fact, it is just a special syntax for literals that tells the compiler to do no interpretation of '\' characters in the string. Unfortunately, the term was coined to describe this compile-time behavior, but many beginners assume it carries some special runtime characteristics.

术语“原始字符串”令人费解,因为它听起来像是一种特殊类型的字符串——而实际上,它只是文字的一种特殊语法,告诉编译器不要解释字符串中的‘\’字符。不幸的是,这个术语是用来描述这种编译时行为的,但是许多初学者认为它带有一些特殊的运行时特征。

I prefer to call them "raw string literals", to emphasize that it is their definition of a string literal using a don't-interpret-backslashes syntax that is what makes them "raw". Both raw string literals and normal string literals create strings (or strs), and the resulting variables are strings like any other. The string created by a raw string literal is equivalent in every way to the same string defined non-raw-ly using escaped backslashes.

我更喜欢称它们为“原始字符串字面量”,以强调它们是用“不带解释的反斜杠语法”来定义字符串字面意义的,而正是这种语法使它们“原始”。原始字符串和普通字符串都创建字符串(或strs),结果变量与其他字符串一样。由原始字符串文字创建的字符串在各方面都与使用转义反斜线定义的非原始字符串等价。


推荐阅读
author-avatar
风云时尚_榜中榜_434
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有