Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28343

Re: get the matched regular expression position in string.

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.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; 'python,': 0.02; 'else:': 0.03; 'output': 0.04; 'string.': 0.04; 'indices': 0.07; 'try:': 0.07; 'valueerror:': 0.07; 'python': 0.09; 'matched': 0.09; 'received:mail-vc0-f174.google.com': 0.09; 'sep': 0.09; 'subject:position': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; '","': 0.16; '[2,': 0.16; 'subject:expression': 0.16; 'subject:regular': 0.16; 'true:': 0.16; 'type:': 0.16; 'mon,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'yield': 0.17; 'pos': 0.22; 'cc:2**0': 0.23; 'example': 0.23; "python's": 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'am,': 0.27; 'necessary.': 0.27; 'message- id:@mail.gmail.com': 0.27; 'regular': 0.27; 'chris': 0.28; 'received:209.85.220.174': 0.29; 'url:python': 0.32; 'print': 0.32; "aren't": 0.33; 'received:google.com': 0.34; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'subject:': 0.36; 'except': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'uses': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'url:docs': 0.38; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'here': 0.65; 'contrary': 0.71; 'future,': 0.72; 'subject:get': 0.81; 'sender:addr:chris': 0.84; '1:18': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=aeuY4GLNUyixN277k7t+BnMqIdMXDWUFpwAALOirD7w=; b=Zp2fxsj/nwl337jfviW+/84FNlb9p1XZO9t8DkdyPUQp9UdVjcoGnpfqK5o/yvRG6T 9B9doYdHC0c4OUmpMzTcAruKJYMoh+kvHc8m5HFVKt/3+EsVLJ/3vGlTyLkOqXK4EAoV NTVhPnKaVEnOElnNUj/Qy+wpVMMRIxaPZ0VIw=
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :x-gm-message-state; bh=aeuY4GLNUyixN277k7t+BnMqIdMXDWUFpwAALOirD7w=; b=HBQmY73HLo4bhvJHSJQZsqb/2ODI0s7hBBuL5gbKF3jf1frIVDxZd15U9wqW3Z6S8O FlaSNsoeWRlyMb2zahKTs7Z3GmGA2U0T0U/LNF944rXgExPGQM3E9otnmdfA4ocT9gjD /fwUm93LSwNZDuvpG+bH+KuIgNXc/fyvdPsgBg/fzOHw5csLn12SOBx7seBjOhZ7x74l XNN5FfzueqLCGD2ExAGb5qrukXK4zu8qexpEVHZeBcEjaF27SQGB4y4z4nhSl87Xw0iw s7BYTII1kO4p8zb5F/kGV85bIqgulAYIN673gKmqmaTqEYKZ/pslKrUgEvnZpJrtlEMa Q7Lw==
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <CA+YdQ_4xRsyPaXgEpc6UEA9NUWoQp3GZGijQYncBStZg5ExYFw@mail.gmail.com>
References <CA+YdQ_4xRsyPaXgEpc6UEA9NUWoQp3GZGijQYncBStZg5ExYFw@mail.gmail.com>
Date Mon, 3 Sep 2012 01:36:12 -0700
X-Google-Sender-Auth rq3wztzmdyyxCHlniHvUc_LB1kY
Subject Re: get the matched regular expression position in string.
From Chris Rebert <clp2@rebertia.com>
To contro opinion <contropinion@gmail.com>
Content-Type text/plain; charset=UTF-8
X-Gm-Message-State ALoCoQmpOteG/8zpSZS9LSiJHosNc5LwsYzVxrv7+z4bPJM+DPwQAWgSsZSJlw6Guv91V3k2HvIE
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.133.1346661375.27098.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1346661375 news.xs4all.nl 6886 [2001:888:2000:d::a6]:56822
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28343

Show key headers only | View raw


On Mon, Sep 3, 2012 at 1:18 AM, contro opinion <contropinion@gmail.com> wrote:
> Subject: get the matched regular expression position in string.

As is often the case in Python, string methods suffice. Particularly
for something so simple, regexes aren't necessary.

> 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.

Note that Python's indices start at 0. Your example output uses 1-based indices.

> how can I get it in python ?

def find_all(string, substr):
    start = 0
    while True:
        try:
            pos = string.index(substr, start)
        except ValueError:
            break
        else:
            yield pos
            start = pos + 1

str1 = "ha,hihi,aaaaa,ok"
print list(find_all(str1, ","))  #===> [2, 7, 13]


In the future, I recommend reading the documentation for Python's string type:
http://docs.python.org/library/stdtypes.html#string-methods

What opinion are you contrary ("contro") to anyway?

Regards,
Chris

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: get the matched regular expression position in string. Chris Rebert <clp2@rebertia.com> - 2012-09-03 01:36 -0700

csiph-web