rawstring

How do I get the raw representation of a string in Python?

孤街醉人 提交于 2019-12-18 04:38:13
问题 I am making a class that relies heavily on regular expressions. Let's say my class looks like this: class Example: def __init__(self, regex): self.regex = regex def __repr__(self): return 'Example({})'.format(repr(self.regex.pattern)) And let's say I use it like this: import re example = Example(re.compile(r'\d+')) If I do repr(example) , I get 'Example('\\\\d+')' , but I want 'Example(r'\\d+')' . Take into account the extra backslash where that upon printing, it appears correctly. I suppose

Raw string and regular expression in Python

て烟熏妆下的殇ゞ 提交于 2019-12-18 04:19:09
问题 I have some confusions regarding raw string in the following code: import re text2 = 'Today is 11/27/2012. PyCon starts 3/13/2013.' text2_re = re.sub(r'(\d+)/(\d+)/(\d+)', r'\3-\1-\2', text2) print (text2_re) #output: Today is 2012-11-27. PyCon starts 2013-3-13. print (r'(\d+)/(\d+)/(\d+)') #output: (\d+)/(\d+)/(\d+) As how I understand the raw string, without r , the \ is treated as escape character; with r , the backslash \ is treated as itself literally. However, what I cannot understand

Raw Strings in Java - for regex in particular

故事扮演 提交于 2019-12-17 08:45:07
问题 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 any way to "simulate" them in any way whatsoever? 回答1: No, there isn't. Generally, you would put raw strings and regexes in a properties file, but those have some escape sequence requirements too. 回答2: This is a work-around if you are using eclipse

escape R“()” in a raw string in C++

Deadly 提交于 2019-12-13 12:04:51
问题 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? 回答1: The format for the raw-string literals[2] is: R"delimiter( raw_characters )delimiter" so you can use a different delimiter that is not in the string like: string raw_str = R"~(R"(foo)")~"; 回答2: The raw string will terminate after the first )" it sees. You can change the delimiter to *** for

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

我的未来我决定 提交于 2019-12-12 09:28:22
问题 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 and why? 回答1: [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

How to bind data in heredoc of scala?

爷,独闯天下 提交于 2019-12-10 18:47:20
问题 val name = "mike" val str = """Hi, {name}!""" println(str) I want it output the str as Hi, mike! , but failed. How to do this? 回答1: Scala does not support string interpolation. There is a compiler plugin that implements it at http://github.com/jrudolph/scala-enhanced-strings. Without the plugin you can use concatenation or format strings: val str = name formatted "Hi, %s!" or of course val str = "Hi, %s!".format(name) 回答2: A total hackish solution would be to use Scala's XML interpolation:

Python raw strings and unicode : how to use Web input as regexp patterns?

自古美人都是妖i 提交于 2019-12-05 06:14:49
问题 EDIT : This question doesn't really make sense once you have picked up what the "r" flag means. More details here. For people looking for a quick anwser, I added on below. If I enter a regexp manually in a Python script, I can use 4 combinations of flags for my pattern strings : p1 = "pattern" p2 = u"pattern" p3 = r"pattern" p4 = ru"pattern" I have a bunch a unicode strings coming from a Web form input and want to use them as regexp patterns. I want to know what process I should apply to the

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

99封情书 提交于 2019-12-04 22:12:41
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 and why? [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

escape R“()” in a raw string in C++

♀尐吖头ヾ 提交于 2019-12-04 04:25:37
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? The format for the raw-string literals [ 2 ] is: R"delimiter( raw_characters )delimiter" so you can use a different delimiter that is not in the string like: string raw_str = R"~(R"(foo)")~"; The raw string will terminate after the first )" it sees. You can change the delimiter to *** for example: string raw_str = R"***(R"(foo)")***"; 来源: https://stackoverflow.com/questions/49416631/escape-r-in-a-raw

Python - Raw String Literals

牧云@^-^@ 提交于 2019-12-01 17:20:36
I don't understand how raw string literals work. I know that when using r it ignores all specials, like when doing \n it treats it as \n and not as a new line. but then I tried to do this: x = r'\' and it said SyntaxError: EOL while scanning string literal and not '\' why? did I understanded it correctly? and also what is the explanation for this : print r'\\' # gives '\\' print r'\\\' # gives SyntaxError The only way to put in a single quote into a string started with a single quote is to escape it. Thus, both raw and regular string literals will allow escaping of quote characters when you