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


Groups > comp.lang.python > #28346 > unrolled thread

Re: get the matched regular expression position in string.

Started byVlastimil Brom <vlastimil.brom@gmail.com>
First post2012-09-03 13:36 +0200
Last post2012-09-03 13:36 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#28346 — Re: get the matched regular expression position in string.

FromVlastimil Brom <vlastimil.brom@gmail.com>
Date2012-09-03 13:36 +0200
SubjectRe: get the matched regular expression position in string.
Message-ID<mailman.136.1346672171.27098.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web