What are the actual uses of ES6 Raw String Access?

The best, and very nearly only, use case for String.raw I can think of is if you’re trying to use something like Steven Levithan’s XRegExp library that accepts text with significant backslashes. Using String.raw lets you write something semantically clear rather than having to think in terms of doubling your backslashes, just like you can … Read more

How to convert a “raw” string into a normal string? [duplicate]

If your input value is a str string, use codecs.decode() to convert: import codecs codecs.decode(raw_unicode_string, ‘unicode_escape’) If your input value is a bytes object, you can use the bytes.decode() method: raw_byte_string.decode(‘unicode_escape’) Demo: >>> import codecs >>> codecs.decode(‘\\x89\\n’, ‘unicode_escape’) ‘\x89\n’ >>> b’\\x89\\n’.decode(‘unicode_escape’) ‘\x89\n’ Python 2 byte strings can be decoded with the ‘string_escape’ codec: >>> import … Read more

How can I put an actual backslash in a string literal (not use it for an escape sequence)?

To answer your question directly, put r in front of the string. final= path + r’\xulrunner.exe ‘ + path + r’\application.ini’ But a better solution would be os.path.join: final = os.path.join(path, ‘xulrunner.exe’) + ‘ ‘ + \ os.path.join(path, ‘application.ini’) (the backslash there is escaping a newline, but you could put the whole thing on one … Read more

In C++11 what should happen first: raw string expansion or macros?

[Update: Adrian McCarthy comments below saying MSVC++ 2017 fixes this] GCC and clang are right, VC++ is wrong. 2.2 Phases of translation [lex.phases]: […] The source file is decomposed into preprocessing tokens (2.5) and sequences of white-space characters (including comments). Preprocessing directives are executed, […] And 2.5 Preprocessing tokens [lex.pptoken] lists string-literals amongst the tokens. … Read more

Include )” in raw string literal without terminating said literal

Raw string literals let you specify an almost arbitrary* delimiter: //choose ### as the delimiter so only )###” ends the string R”###( Some Text)” )###”; *The exact rules are: “any member of the basic source character set except: space, the left parenthesis (, the right parenthesis ), the backslash \, and the control characters representing … Read more

How to match a newline character in a raw string?

In a regular expression, you need to specify that you’re in multiline mode: >>> import re >>> s = “””cat … dog””” >>> >>> re.match(r’cat\ndog’,s,re.M) <_sre.SRE_Match object at 0xcb7c8> Notice that re translates the \n (raw string) into newline. As you indicated in your comments, you don’t actually need re.M for it to match, but … Read more

What is a raw string?

Raw string literals are string literals that are designed to make it easier to include nested characters like quotation marks and backslashes that normally have meanings as delimiters and escape sequence starts. They’re useful for, say, encoding text like HTML. For example, contrast “<a href=\”file\”>C:\\Program Files\\</a>” which is a regular string literal, with R”(<a href=”https://stackoverflow.com/questions/56710024/file”>C:\Program … Read more