How can I get text within a specific text?
Like, if I have a text like this:
'lololol \r asdfasdf r\ gfhfgr'
How can I get it to return ‘asdfasdf’. Basically, get the text between the bits '\r' and 'r\'?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Another re solution,
results in
['asdfasdf']Edit:
Well, dude, that’s what you said you wanted in your example. If you prefer, you are welcome to use
which will instead give you
[' asdfasdf '].‘All the slashes’ are needed because of the way Python and re parse strings; a slash is used as an escape character for digraphs like
\n(newline character). So to denote a slash, you have to use the\\digraph. Tryprint('\\')to see this.You then have to double it, because re parses the string again against its own set of digraphs (\d for digits, \s for whitespace, etc). So if you enter
'\\\\', Python understand this to be a string composed of two slashes,\\, which it passes to re, which parses it and thinks you are looking for a single\character.Sometimes you can get away without doing this; if you have a string like
'\m', where the trailing char does not result in a valid digraph, the result is actually the two-character string\m(tryprint('\m')). \r is kind of funky; Python recognizes it as a carriage-return digraph, but re does not use \r as a digraph, so giving Python ‘\r’ or ‘\\r’ both result in re looking for the literal string ‘\r’. I prefer the double-double-slash, as this means you don’t have to remember two separate definitions of what is or isn’t a legal digraph! On the other hand, both Python and re recognize \’ as a single-quote digraph (print('\'')prints a'character) – so both slashes have to be fully double-escaped or you’ll get a “Hey, where’s the rest of the string??” error (‘string not terminated’).The other alternative is to enter raw strings (
r'abc'); this tells Python not to parse digraphs in the string, but re will still do so, so your pattern has to look like