Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.04; 'assign': 0.04; 'escape': 0.04; 'string.': 0.04; 'lines.': 0.05; 'next,': 0.05; 'character,': 0.07; 'forth.': 0.07; 'interpreter': 0.07; 'tab': 0.07; 'whatever.': 0.07; 'python': 0.08; "'this": 0.09; '>>>>': 0.09; 'backslash': 0.09; 'i.e.,': 0.09; 'interpreter,': 0.09; 'newline': 0.09; 'newline,': 0.09; 'parsing': 0.09; 'return,': 0.09; 'syntax.': 0.09; 'variables,': 0.09; 'displayed': 0.10; 'def': 0.15; 'hopefully': 0.15; 'tries': 0.15; '"\\\\"': 0.16; 'backslash,': 0.16; 'backslashes': 0.16; 'delimit': 0.16; 'escapes': 0.16; 'executed.': 0.16; 'experiment.': 0.16; 'function;': 0.16; 'likewise': 0.16; 'literal,': 0.16; 'literal.': 0.16; 'literals': 0.16; 'received:192.168.1.104': 0.16; 'repr()': 0.16; 'row.': 0.16; 'subject:regular': 0.16; 'versa.': 0.16; 'cc:addr:python-list': 0.16; 'syntax': 0.16; 'wrote:': 0.16; 'meant': 0.17; '>>>': 0.18; 'converted': 0.18; 'example.': 0.18; 'purposes,': 0.18; 'string,': 0.18; 'cc:no real name:2**0': 0.20; 'seems': 0.20; 'figure': 0.21; 'trying': 0.21; 'file,': 0.21; 'result.': 0.21; "doesn't": 0.22; 'stuff': 0.22; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'literal': 0.23; 'similarly': 0.23; 'pm,': 0.24; 'input': 0.24; 'says': 0.25; 'string': 0.26; 'thanks.': 0.26; "i'm": 0.27; 'function': 0.27; '(the': 0.28; 'code,': 0.28; 'separate': 0.28; 'problem': 0.28; 'unknown': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.30; 'lines': 0.30; 'not.': 0.30; 'turned': 0.30; 'characters,': 0.30; 'completed.': 0.30; 'confident': 0.30; 'confused': 0.30; 'differently': 0.30; 'object.': 0.30; 'strings.': 0.30; 'unable': 0.31; 'represents': 0.32; 'actual': 0.32; 'does': 0.32; 'usually': 0.32; "isn't": 0.33; "what's": 0.33; 'it.': 0.33; 'probably': 0.33; "can't": 0.33; 'there': 0.33; 'calling': 0.33; 'decide': 0.33; 'regardless': 0.34; '...': 0.34; 'header:User-Agent:1': 0.34; 'be.': 0.34; 'quite': 0.34; 'file.': 0.34; 'test': 0.34; 'requirements': 0.34; 'function.': 0.34; 'latter': 0.34; 'puts': 0.34; 'uses': 0.35; 'running': 0.35; 'object': 0.35; 'unless': 0.36; 'doing': 0.36; 'skip:" 10': 0.36; 'aspect': 0.37; 'variables': 0.37; 'using': 0.37; 'put': 0.37; 'but': 0.37; 'something': 0.37; 'two': 0.37; 'some': 0.38; 'subject:: ': 0.39; 'received:192': 0.39; 'characters': 0.39; 'vice': 0.39; 'represent': 0.39; 'ways': 0.39; 'help': 0.39; 'either': 0.39; 'your': 0.61; 'double': 0.61; 'kind': 0.61; 'subject': 0.61; 'results': 0.61; 'order': 0.62; 'you.': 0.62; 'here': 0.65; 'details': 0.65; 'ready': 0.65; 'header:Reply-To:1': 0.71; 'reply- to:no real name:2**0': 0.71; 'funny': 0.74; 'escaping': 0.84; 'experiment': 0.84 Date: Wed, 26 Oct 2011 16:47:54 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110922 Thunderbird/3.1.15 MIME-Version: 1.0 To: Ross Boylan Subject: Re: inserting \ in regular expressions References: <1319658482.13425.9.camel@corn.betterworld.us> In-Reply-To: <1319658482.13425.9.camel@corn.betterworld.us> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:xzE6SW/7SN6HjM+1t0EynVT7aZxzxdHTe2AqIveU9GS +681Vgi0tSYJc3L4fOnKcZa4PoKLJMz4kYUhUzx+EEEjWWjzf9 UCNp0shILBMghxqBN6mRxfCzfKfWyZbqOt+cxVTJZo1e5Z0NMX TqEHpBULx+BQsEJZ/hAxPUQvwY8DNfUssrZaK0cI8teE3vRdA1 d9brMn0J7UQYyKonBro0YFjtgVj+AXNIhyKEWHzSd3n2M0lXsk 52UCravw233Zpy5lhfs/rzJYPfht4UgxJgHvpnQeIWEIU93VGp JDdwxkecFow9TGA1XVisE26sb3zHiXNsWdQsxAge0fiXpr6EA= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: d@davea.name List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 112 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1319662171 news.xs4all.nl 6862 [2001:888:2000:d::a6]:54437 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15014 On 10/26/2011 03:48 PM, Ross Boylan wrote: > I want to replace every \ and " (the two characters for backslash and > double quotes) with a \ and the same character, i.e., > \ -> \\ > " -> \" > > I have not been able to figure out how to do that. The documentation > for re.sub says "repl can be a string or a function; if it is a string, > any backslash escapes in it are processed.That is, \n is converted to a > single newline character, \r is converted to a carriage return, and so > forth. Unknown escapes such as \j are left alone." > > \\ is apparently unknown, and so is left as is. So I'm unable to get a > single \. > > Here are some tries in Python 2.5.2. The document suggested the result > of a function might not be subject to the same problem, but it seems to > be. >>>> def f(m): > ... return "\\"+m.group(1) > ... >>>> re.sub(r"([\\\"])", f, 'Silly " quote') > 'Silly \\" quote' > >>> re.sub(r"([\\\"])", "\\\\\\1", 'Silly " quote') > 'Silly \\" quote' > > Or perhaps I'm confused about what the displayed results mean. If a > string has a literal \, does it get shown as \\? > > I'd appreciate it if you cc me on the reply. > > Thanks. > Ross Boylan > I can't really help on the regex aspect of your code, but I can tell you a little about backslashes, quote literals, the interpreter, and python. First, I'd scrap the interpreter and write your stuff to a file. Then test it by running that file. The reason for that is that the interpreter is helpfully trying to reconstruct the string you'd have to type in order to get that result. So while you may have successfully turned a double bacdkslash into a single one, the interpreter helpfully does the inverse, and you don't see whether you're right or not. Next, always assign to variables, and test those variables on a separate line with the regex. This is probably what your document meant when it mentioned the result of a function. Now some details about python. When python compiles/interprets a quote literal, the syntax parsing has to decide where the literal stops, so quotes are treated specially. Sometimes you can sidestep the problem of embedding quotes inside literals by using single quotes on the outside and double inside, or vice versa. As you did on the 'Silly " quote' example. But the more general way to put funny characters into a quote literal is to escape each with a backslash. So there a bunch of two-character escapes. backslash-quote is how you can put either kind of quote into a literal, regardless of what's being used to delimit it. backslash-n gets a newline, which would similarly be bad syntax. backslash-t and some others are usually less troublesome, but can be surprising. And backslash-backslash represents a single backslash. There are also backslash codes to represent arbitrary characters you might not have on your keyboard. And these may use multiple characters after the backslash. So write a bunch of lines like a = 'this is\'nt a surprise' print a and experiment. Notice that if you use \n in such a string, the print will put it on two lines. Likewise the tab is executed. Now for a digression. The interpreter uses repr() to display strings. You can experiment with that by doing print a print repr(a) Notice the latter puts quotes around the string. They are NOT part of the string object in a. And it re-escapes any embedded funny characters, sometimes differently than the way you entered them. Now, once you're confident that you can write a literal to express any possible string, try calling your regex. print re.sub(a, b, c) or whatever. Now, one way to cheat on the string if you know you'll want to put actual backslashes is to use the raw string. That works quite well unless you want the string to end with a backslash. There isn't a way to enter that as a single raw literal. You'd have to do something string like a = r"strange\literal\with\some\stuff" + "\\" My understanding is that no valid regex ends with a backslash, so this may not affect you. Now there are other ways to acquire a string object. If you got it from a raw_input() call, it doesn't need to be escaped, but it can't have an embedded newline, since the enter key is how the input is completed. If you read it from a file, it doesn't need to be escaped. Now you're ready to see what other funny requirements regex needs. You will be escaping stuff for their purposes, and sometimes that means your literal might have 4 or even more backslashes in a row. But hopefully now you'll see how to separate the different problems. -- DaveA