Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #15016
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python@mrabarnett.plus.com> |
| 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; 'escape': 0.04; 'character,': 0.07; 'forth.': 0.07; 'backslash': 0.09; 'from:addr:python': 0.09; 'i.e.,': 0.09; 'newline': 0.09; 'return,': 0.09; 'backspace': 0.16; 'escapes': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'function;': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject:regular': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'converted': 0.18; 'string,': 0.18; 'figure': 0.21; 'header:In-Reply-To:1': 0.22; 'says': 0.25; 'string': 0.26; "i'm": 0.27; '(the': 0.28; 'received:84': 0.28; 'unknown': 0.29; 'print': 0.29; 'skip:( 20': 0.29; 'unable': 0.31; 'to:addr:python- list': 0.33; 'header:User-Agent:1': 0.34; 'character': 0.34; 'two': 0.37; 'subject:: ': 0.39; 'characters': 0.39; 'to:addr:python.org': 0.39; 'double': 0.61; 'escaping': 0.84 |
| X-CM-Score | 0.00 |
| X-CNFS-Analysis | v=2.0 cv=NdNkJh/4 c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=yj_6LXTI0WAA:10 a=jdTnPxqkDa4A:10 a=8nJEP1OIZ-IA:10 a=-YivkN7_hlKQEksqYIYA:9 a=q4y-9wNa9kPjMKpwWUIA:7 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 |
| X-AUTH | mrabarnett:2500 |
| Date | Wed, 26 Oct 2011 23:38:37 +0100 |
| From | MRAB <python@mrabarnett.plus.com> |
| User-Agent | Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| 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-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2241.1319668792.27778.python-list@python.org> (permalink) |
| Lines | 23 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1319668792 news.xs4all.nl 6912 [2001:888:2000:d::a6]:51384 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:15016 |
Show key headers only | View raw
On 26/10/2011 20:48, 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 \.
>
[snip]
The backspace character is also used for escaping in a regex, so you
need to escape it with a backslash:
>>> print('Silly " quote or \\ backslash')
Silly " quote or \ backslash
>>> print (re.sub(r'([\\\\"])', r"\\\1", 'Silly " quote or \\ backslash'))
Silly \" quote or \\ backslash
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: inserting \ in regular expressions MRAB <python@mrabarnett.plus.com> - 2011-10-26 23:38 +0100
csiph-web