I haven't tested it either but it looks like it would work. But for this case I prefer the relative simplicity of:
example = 'X - abc_degree + 1 + qq + abc_degree + 1' find_string = re.escape('abc_degree + 1') for match in re.finditer(find_string, example): print(match.start(), match.end()) 4 18 26 40 I don't insist on terseness for its own sake, but it's cleaner this way. Jen Feb 27, 2023, 16:55 by c...@cskk.id.au: > On 28Feb2023 01:13, Jen Kris <jenk...@tutanota.com> wrote: > >> I went to the re module because the specified string may appear more than >> once in the string (in the code I'm writing). >> > > Sure, but writing a `finditer` for plain `str` is pretty easy (untested): > > pos = 0 > while True: > found = s.find(substring, pos) > if found < 0: > break > start = found > end = found + len(substring) > ... do whatever with start and end ... > pos = end > > Many people go straight to the `re` module whenever they're looking for > strings. It is often cryptic error prone overkill. Just something to keep in > mind. > > Cheers, > Cameron Simpson <c...@cskk.id.au> > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list