Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'modified': 0.07; 'subject:PEP': 0.07; 'users,': 0.07; 'string': 0.09; 'pep': 0.09; 'def': 0.12; '"hello': 0.16; 'collins': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'itertools': 0.16; 'literal,': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'think?': 0.16; 'wrote:': 0.18; 'library': 0.18; 'import': 0.22; 'this?': 0.23; 'header :User-Agent:1': 0.23; 'propose': 0.24; 'skip:" 30': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; "i'm": 0.30; 'work:': 0.31; 'class': 0.32; 'another': 0.32; 'text': 0.33; 'cases': 0.33; 'checking': 0.33; 'entirely': 0.33; 'skip:_ 10': 0.34; 'something': 0.35; 'there': 0.35; 'doing': 0.36; 'should': 0.36; 'so,': 0.37; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'users': 0.40; 'how': 0.40; 'chain': 0.60; 'new': 0.61; 'simple': 0.61; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'reply-to:addr:python.org': 0.84; 'subject:Possible': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=C6LQl2/+ c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=OJn0C7AadrgA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=NMJKD89GzEwA:10 a=1-y7I_UWZ66qGHw-CAEA:9 a=wPNLvfGTeEIA:10 X-AUTH: mrabarnett:2500 Date: Mon, 09 Dec 2013 21:08:43 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.1.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Possible PEP Submission References: In-Reply-To: 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.15 Precedence: list Reply-To: python-list@python.org 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: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386623316 news.xs4all.nl 2854 [2001:888:2000:d::a6]:39255 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61417 On 09/12/2013 20:36, Logan Collins wrote: > Just checking whether 1) a PEP is the proper place for this and 2) what > y'all think about it. > > I would like to propose a change to the the 're' standard library to > support iterables. > > So, something like the following would work: > > import re > text = """hello user > hello user > hello user""" > > users = ["Bob", "Alice", "Jeffery"] > > newtext = re.sub("user", users, text) > > # newtext = "hello Bob\nhello Alice\nhello Jeffery" > > There are a few corner cases I'm not sure what would be the best > behavior. Nor am I entirely certain this should be modified > functionality or just... a new function. What do y'all think? > What's your use-case? How often would someone want to do this? Here's a way of doing it without adding to the re module: import re text = """hello user hello user hello user""" class Replacement: def __init__(self, users): self.users = users self.index = -1 def __call__(self, m): self.index += 1 return self.users[self.index] users = ["Bob", "Alice", "Jeffery"] newtext = re.sub("user", Replacement(users), text) # newtext = "hello Bob\nhello Alice\nhello Jeffery" print(newtext) The search string is a simple literal, so another way is: from itertools import chain text = """hello user hello user hello user""" users = ["Bob", "Alice", "Jeffery"] # newtext = "hello Bob\nhello Alice\nhello Jeffery" newtext = "".join(chain(*zip(text.split("user", len(users)), users))) print(newtext)