max_noodels max_noodels - 7 months ago 15
Python Question

Finding the index of charaters in a string

dave = [m.start() for m in re.finditer('*', "2345234*265354*26342567*356")]

Whenever I run this piece of code it gives me this huge error.

Traceback (most recent call last):
File "C:\Users\Max\Desktop\MaxsCal", line 107, in <module>
dave = [m.start() for m in re.finditer('*', "2345234*265354*26342567*356")]
File "C:\Program Files (x86)\Python 3\lib\", line 220, in finditer
return _compile(pattern, flags).finditer(string)
File "C:\Program Files (x86)\Python 3\lib\", line 293, in _compile
p = sre_compile.compile(pattern, flags)
File "C:\Program Files (x86)\Python 3\lib\", line 536, in compile
p = sre_parse.parse(p, flags)
File "C:\Program Files (x86)\Python 3\lib\", line 829, in parse
p = _parse_sub(source, pattern, 0)
File "C:\Program Files (x86)\Python 3\lib\", line 437, in _parse_sub
itemsappend(_parse(source, state))
File "C:\Program Files (x86)\Python 3\lib\", line 638, in _parse
source.tell() - here + len(this))
sre_constants.error: nothing to repeat at position 0

Is there anyway to fix the code to give the output, which is meant to be the index of all the
, of something like
[7, 14, 23]


re.finditer() uses a regular expression to find matches. That regular expression is '*' in your case. The * has a special meaning in regular expressions, so if you just mean to search for a star character, you need to escape it:

dave = [m.start() for m in re.finditer('\\*', "2345234*265354*26342567*356")]