Questions tagged [rawstring]

A string literal which would be processed without any language-specific interpretation, avoiding the need of escaping characters and thus providing more legible strings.

Raw strings are particularly useful when a common character needs to be escaped, notably in regular expressions (nested as string literals), where backslash \ is widely used, and in DOS/Windows paths, where backslash is used as a path separator.

114 questions
719
votes
7 answers

What exactly do "u" and "r" string flags do, and what are raw string literals?

While asking this question, I realized I didn't know much about raw strings. For somebody claiming to be a Django trainer, this sucks. I know what an encoding is, and I know what u'' alone does since I get what is Unicode. But what does r'' do…
e-satis
  • 515,820
  • 103
  • 283
  • 322
208
votes
12 answers

Why can't Python's raw string literals end with a single backslash?

Technically, any odd number of backslashes, as described in the documentation. >>> r'\' File "", line 1 r'\' ^ SyntaxError: EOL while scanning string literal >>> r'\\' '\\\\' >>> r'\\\' File "", line 1 r'\\\' …
cdleary
  • 63,281
  • 49
  • 155
  • 190
144
votes
2 answers

What does preceding a string literal with "r" mean?

I first saw it used in building regular expressions across multiple lines as a method argument to re.compile(), so I assumed that r stands for RegEx. For example: regex = re.compile( r'^[A-Z]' r'[A-Z0-9-]' r'[A-Z]$', re.IGNORECASE ) So…
Nikki Erwin Ramirez
  • 8,866
  • 6
  • 27
  • 30
77
votes
7 answers

What exactly is a "raw string regex" and how can you use it?

From the python documentation on regex, regarding the '\' character: The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So…
temporary_user_name
  • 30,801
  • 41
  • 120
  • 186
76
votes
12 answers

Raw Strings in Java - for regex in particular. Multiline strings

Is there any way to use raw strings in Java (without escape sequences)? (I'm writing a fair amount of regex code and raw strings would make my code immensely more readable) I understand that the language does not provide this directly, but is there…
Debajit
  • 43,330
  • 32
  • 87
  • 97
46
votes
3 answers

Include )" in raw string literal without terminating said literal

The two characters )" terminate the raw string literal in the example below. The sequence )" could appear in my text at some point, and I want the string to continue even if this sequence is found within it. R"( Some Text)" )"; //…
Andreas DM
  • 9,654
  • 6
  • 31
  • 58
44
votes
2 answers

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

This code works in Visual C++ 2013 but not in gcc/clang: #if 0 R"foo( #else int dostuff () { return 23; } // )foo"; #endif dostuff(); Visual C++ removes the if 0 first. Clang expands the R raw string first (and never defining dostuff). Who is right…
starmole
  • 4,680
  • 21
  • 46
43
votes
2 answers

Valid JSON giving JSONDecodeError: Expecting , delimiter

I'm trying to parse a json response data from youtube api but i keep getting an error. Here is the snippet where it choking: data = json.loads("""{ "entry":{ "etag":"W/\"A0UGRK47eCp7I9B9WiRrYU0.\"" } }""") ..and this happens: JSONDecodeError:…
userBG
  • 5,610
  • 8
  • 28
  • 36
39
votes
4 answers

How to match a new line character in Python raw string

I got a little confused about Python raw string. I know that if we use raw string, then it will treat '\' as a normal backslash (ex. r'\n' would be \ and n). However, I was wondering what if I want to match a new line character in raw string. I…
wei
  • 2,782
  • 3
  • 20
  • 31
30
votes
6 answers

What are the actual uses of ES6 Raw String Access?

What are the actual uses of String.raw Raw String Access introduced in ECMAScript 6? // String.raw(callSite, ...substitutions) function quux (strings, ...values) { strings[0] === "foo\n" strings[1] === "bar" strings.raw[0] === "foo\\n" …
Venkat.R
  • 6,616
  • 5
  • 34
  • 57
22
votes
2 answers

How to convert a "raw" string into a normal string?

In Python, I have a string like this: '\\x89\\n' How can I decode it into a normal string like: '\x89\n'
Wenlin.Wu
  • 730
  • 1
  • 8
  • 17
20
votes
2 answers

How to format raw string with different expressions inside?

Let's say, we want to catch something with regex, using rawstring to define the pattern, which pattern has repeating elements, and variables inside. And we also want to use the format() string formatting form. How to do this? import re text =…
Peter Varo
  • 10,078
  • 6
  • 42
  • 63
16
votes
3 answers

What is a raw string?

I came across this code snippet in C++17 draft n4713: #define R "x" const char* s = R"y"; // ill-formed raw string, not "x" "y" What is a "raw string"? What does it do?
S.S. Anne
  • 13,819
  • 7
  • 31
  • 62
16
votes
3 answers

Does clojure have raw string?

In Python, I can prefix an r to a string literal (raw string) to tell the interpreter not translate special characters in the string: >>> r"abc\nsdf#$%\^" r"abc\nsdf#$%\^" Is there a way to do the same thing in Clojure?
John Wang
  • 4,114
  • 6
  • 31
  • 48
13
votes
2 answers

escape R"()" in a raw string in C++

string raw_str = R"(R"(foo)")"; If I have R"()" inside a raw string, and that causes the parser to confuse. (ie., it thought the left most )" was the end of the raw string. How do I escape this?
One Two Three
  • 18,931
  • 22
  • 63
  • 100
1
2 3 4 5 6 7 8