-1

Python strings appear to be missing a function that returns the index of all matches.

There is a re.findall() in the Python module that supplies the power of regular expressions within the standard library. But using the re module to find all occurrences of one string in another seems excessive to me. There's no need for regular expressions in this case.

fabianegli
  • 2,056
  • 1
  • 18
  • 35

1 Answers1

0

One solution is a custom findall() function to that finds all occurrences of a string. It is a mix of the methods of the re.findall() and the str.find() method and returns a list indexes where all the matches start.

from typing import Optional

def findall(
    pattern: str, string: str, start: Optional[int] = None, end: Optional[int] = None
):
    """Find all matches of a pattern in a string.

    Arguments
    ---------
    pattern : str
        The string to find.
    string : str
        The string to search in.
    start : int or None
        Where in the string to start the search for matches.
    end : int or None
        Where in the string to end the search for matches.
    
    Returns
    -------
    list
        A list of of the index of the start of all matches.
        If no matches are found the list is empty.
    """

    i = string.find(pattern, start, end)

    if i == -1:
        return []
    else:
        return [i] + findall(pattern, string, i + 1, end)
fabianegli
  • 2,056
  • 1
  • 18
  • 35
  • You're not entirely wrong with the "You don't have a question." But it boggles my mind that Python strings have 45 methods and not one to find all occurrences of another string. If this gets enough attention, it might help getting such a function into the standard library? So the question really is "Why don't Python strings have a findall method?" – fabianegli Apr 12 '22 at 15:56
  • Perhaps contact the Python developers to inquire about having something added to the standard library. The [main repo](https://github.com/python/cpython/) is on GitHub which supported bug tracking and issues. That will attract more 'focused' attention than a 'random' post on a *massive* general programmers forum. – S3DEV Apr 12 '22 at 16:00
  • 1
    My feeling is that, whatever your actual use-case may be, there's probably another, more idiomatic option than working with indices. – Paul M. Apr 12 '22 at 16:02
  • Further to the point by @PaulM ... it'll *very* likely be in C. ;-) – S3DEV Apr 12 '22 at 16:06
  • @S3DEV Thank you for the pointer to the cpython repo. – fabianegli Apr 12 '22 at 16:06
  • @paul-m indices is what find eats and returns. The function doesn't work any more with indices than the find method itself. The most elegant and performant solution is probably this one in the linked Q&A https://stackoverflow.com/a/34445090/6018688 – fabianegli Apr 12 '22 at 16:06