Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed6.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.021 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'indices': 0.07; 'python': 0.09; 'subject:position': 0.09; '","': 0.16; '[2,': 0.16; 'subject:expression': 0.16; 'subject:regular': 0.16; 'string': 0.17; '>>>': 0.18; 'respective': 0.20; 'import': 0.21; 'received:209.85.216.46': 0.21; 'header:In-Reply-To:1': 0.25; 'skip:" 20': 0.26; 'message-id:@mail.gmail.com': 0.27; 'url:mailman': 0.29; 'url:python': 0.32; 'url:listinfo': 0.32; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'url:org': 0.36; 'url:library': 0.36; 'method': 0.36; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'url:mail': 0.40; 'here': 0.65; 'obtained': 0.71; 'subject:get': 0.81 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=KYEkdgk+ul6h/cz4kHEa0oQ/TtpWOJoKs5esnDb8S2U=; b=AvA9SxVHlsHGcRyAWztcN8Y7KKScTRM2ZQhXURyDEkXGPz35SGHsm4NLvI+OVOeZR1 TSyY3HJK5o3FCn2aXCls50J+KzhJqzubnivWPIRkA2toECeowNabbqpyhW58QOra5OZu lE0s8x9lJ9KQekiMxxnSXceF854ZiLg2xUp6XUf72WcVDu9N41wszv+d7nz0uhkkHSBm pAb8FWutkTV6tEkWKqQYEWLjx093JrPI6H50Tutuj9snQE+AA1F/Brh5DxDJ/AJR1113 DdDNRFoeojknEw6Gb5mSOHrmwTu18vAbT5YwTjC+YnK6hlyIUI6Ob3gTOmJ1/blKq8BT +mHw== MIME-Version: 1.0 In-Reply-To: References: Date: Mon, 3 Sep 2012 13:36:01 +0200 Subject: Re: get the matched regular expression position in string. From: Vlastimil Brom To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346672171 news.xs4all.nl 6844 [2001:888:2000:d::a6]:50173 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28346 2012/9/3 contro opinion : > Here is a string : > str1="ha,hihi,aaaaa,ok" > I want to get the position of "," in the str1,Which can count 3,8,14. > how can I get it in python ? > -- > http://mail.python.org/mailman/listinfo/python-list > Hi, you can use re.finditer to match all "," and the start() method of the respective matches; cf.: http://docs.python.org/library/re.html#match-objects >>> import re >>> [m.start() for m in re.finditer(r",", "ha,hihi,aaaaa,ok")] [2, 7, 13] >>> [The obtained indices are zero-based, as has already been mentioned.] hth, vbr