Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'patterns.': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'wrote': 0.14; "wouldn't": 0.14; 'received:80.91.229.3': 0.16; 'received:97': 0.16; 'received:plane.gmane.org': 0.16; 'import': 0.22; 'print': 0.22; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'see,': 0.30; "skip:' 10": 0.31; 'piece': 0.31; 'spam,': 0.31; 'guess': 0.33; 'raw': 0.33; "i'd": 0.34; "i'll": 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'that,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'email addr:gmail.com': 0.63; 'subject:here': 0.84; 'subject::': 0.85 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re:What is \1 here? Date: Tue, 11 Nov 2014 07:22:44 -0500 (EST) Organization: news.gmane.org References: <6720bebb-8263-4fb1-bcef-10dcfdc44905@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 97.73.240.77 X-Newsreader: PiaoHong.Usenet.Client.VIP:1.64 X-Archive: encrypt X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415708571 news.xs4all.nl 2874 [2001:888:2000:d::a6]:53923 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80523 satishmlmlml@gmail.com Wrote in message: > What does \1 do in the following piece of code(fourth line)? > import re > print(re.sub('[ABC]', '*', 'XAXAXBXBXCXC')) > print(re.sub('[ABC]_', '*', 'XA-XA_XB-XB_XC-XC_')) > print(re.sub('(.) spam', 'spam\\1', 'x spam, y spam')) > def mapper(matchobj): > return 'spam' + matchobj.group(1) > print(re.sub('(.) spam', mapper, 'x spam, y spam')) > > Recommend you use raw strings when defining regex patterns. Then you wouldn't have to double the backslash. To see what re.sub will see, try print ('spam\\1') As for what the function does with that, I'll have to guess, as I don't do regex. I'd guess it'll match any digit. -- DaveA