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.

143 questions
858
votes
7 answers

What exactly do "u" and "r" string prefixes 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…
Bite code
  • 578,959
  • 113
  • 301
  • 329
258
votes
14 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
  • 69,512
  • 53
  • 163
  • 191
169
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
  • 9,676
  • 6
  • 30
  • 32
144
votes
6 answers

How to write string literals in Python without having to escape them?

Is there a way to declare a string variable in Python such that everything inside of it is automatically escaped, or has its literal character value? I'm not asking how to escape the quotes with slashes, that's obvious. What I'm asking for is a…
kjakeb
  • 6,810
  • 5
  • 20
  • 34
114
votes
7 answers

How to include a double-quote and/or single-quote character in a raw Python string literal?

Consider: >>> r"what"ever" SyntaxError: invalid syntax >>> r"what\"ever" 'what\\"ever' So how do we get the quote, but not the slash? And please don't suggest r'what"ever', because then the question just becomes how do we include both types of…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
91
votes
13 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
  • 46,327
  • 33
  • 91
  • 100
89
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
  • 35,956
  • 47
  • 141
  • 220
70
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
  • 6,860
  • 10
  • 31
  • 39
60
votes
4 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
  • 15,171
  • 8
  • 38
  • 76
55
votes
5 answers

How to match a newline character in a 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
  • 3,312
  • 4
  • 23
  • 33
50
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
  • 10,685
  • 6
  • 35
  • 62
46
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,974
  • 1
  • 28
  • 48
41
votes
4 answers

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

I have this code: import os path = os.getcwd() final = path +'\xulrunner.exe ' + path + '\application.ini' print(final) I want output like: C:\Users\me\xulrunner.exe C:\Users\me\application.ini But instead I get an error that looks…
esafwan
  • 17,311
  • 33
  • 107
  • 166
37
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
  • 11,726
  • 7
  • 55
  • 77
32
votes
7 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
  • 7,420
  • 5
  • 42
  • 63
1
2 3
9 10