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


Groups > comp.lang.python > #28346

Re: get the matched regular expression position in string.

References <CA+YdQ_4xRsyPaXgEpc6UEA9NUWoQp3GZGijQYncBStZg5ExYFw@mail.gmail.com>
Date 2012-09-03 13:36 +0200
Subject Re: get the matched regular expression position in string.
From Vlastimil Brom <vlastimil.brom@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.136.1346672171.27098.python-list@python.org> (permalink)

Show all headers | View raw


2012/9/3 contro opinion <contropinion@gmail.com>:
> 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

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


Thread

Re: get the matched regular expression position in string. Vlastimil Brom <vlastimil.brom@gmail.com> - 2012-09-03 13:36 +0200

csiph-web