How to find all occurrences of a substring?

python find all occurrences in string regex
find all occurrences of a character in a string python
find all occurrences of a substring in a string javascript
python count all occurrences in string
python find index of all occurrences in list
find all occurrences of a substring in a string c++
python find substring in string
find all substrings of a string python

Python has string.find() and string.rfind() to get the index of a substring in a string.

I'm wondering whether there is something like string.find_all() which can return all found indexes (not only the first from the beginning or the first from the end).

For example:

string = "test test test test"

print string.find('test') # 0
print string.rfind('test') # 15

#this is the goal
print string.find_all('test') # [0,5,10,15]

There is no simple built-in string function that does what you're looking for, but you could use the more powerful regular expressions:

import re
[m.start() for m in re.finditer('test', 'test test test test')]
#[0, 5, 10, 15]

If you want to find overlapping matches, lookahead will do that:

[m.start() for m in re.finditer('(?=tt)', 'ttt')]
#[0, 1]

If you want a reverse find-all without overlaps, you can combine positive and negative lookahead into an expression like this:

search = 'tt'
[m.start() for m in re.finditer('(?=%s)(?!.{1,%d}%s)' % (search, len(search)-1, search), 'ttt')]
#[1]

re.finditer returns a generator, so you could change the [] in the above to () to get a generator instead of a list which will be more efficient if you're only iterating through the results once.

Python, This may include the problem of finding all positions of a particular substrings in a string. Let's discuss certain ways in which this task can be performed. Method #1​  The original string is : GeeksforGeeks is best for Geeks The substring to find : Geeks The start indices of the substrings are : [0, 8, 26] Method #2 : Using re.finditer() The finditer function of the regex library can help us perform the task of finding the occurrences of the substring in the target string and the start function can return the resultant index of each of them.

>>> help(str.find)
Help on method_descriptor:

find(...)
    S.find(sub [,start [,end]]) -> int

Thus, we can build it ourselves:

def find_all(a_str, sub):
    start = 0
    while True:
        start = a_str.find(sub, start)
        if start == -1: return
        yield start
        start += len(sub) # use start += 1 to find overlapping matches

list(find_all('spam spam spam spam', 'spam')) # [0, 5, 10, 15]

No temporary strings or regexes required.

How to find all occurrences of a substring?, There is no simple built-in string function that does what you're looking for, but you could use the more powerful regular expressions: import re [m.start() for m in​  Find All Case Sensitive Occurrences of a Sub String in a given String Logic: Use std::string::find to search the first occurrence and then go on searching using same logic from that position onward, till you reach the end.

Here's a (very inefficient) way to get all (i.e. even overlapping) matches:

>>> string = "test test test test"
>>> [i for i in range(len(string)) if string.startswith('test', i)]
[0, 5, 10, 15]

Function to find all occurrences of substring, I would turn this function into a generator so that an iteration over its values does not unnecessarily build a list into memory. If the caller trully  Dim phrase as String = "hello there" Dim Occurrences As Integer = 0 If input.toLower.Contains(phrase) = True Then Occurrences = input.Split(phrase).Length 'REM: Do stuff End If Unfortunately, what this line of code seems to do is split the string every time it sees the first letter of phrase , in this case, h .

Again, old thread, but here's my solution using a generator and plain str.find.

def findall(p, s):
    '''Yields all the positions of
    the pattern p in the string s.'''
    i = s.find(p)
    while i != -1:
        yield i
        i = s.find(p, i+1)
Example
x = 'banananassantana'
[(i, x[i:i+2]) for i in findall('na', x)]

returns

[(2, 'na'), (4, 'na'), (6, 'na'), (14, 'na')]

How to find all occurrences of a substring?, To find all occurrences of a substring Python does not have any built-in string function that does what you're looking for, but you could use the  I have a large string I need to parse, and I need to find all the instances of extract"(me,i-have lots. of]punctuation, and store the index of each to a list.. So say this piece of string was in the beginning and middle of the larger string, both of them would be found, and their indexes would be added to the List. and the List would contain 0 and the other index whatever it would be.

You can use re.finditer() for non-overlapping matches.

>>> import re
>>> aString = 'this is a string where the substring "is" is repeated several times'
>>> print [(a.start(), a.end()) for a in list(re.finditer('is', aString))]
[(2, 4), (5, 7), (38, 40), (42, 44)]

but won't work for:

In [1]: aString="ababa"

In [2]: print [(a.start(), a.end()) for a in list(re.finditer('aba', aString))]
Output: [(0, 3)]

Using indexOf to Find All Occurrences of a Word in a String, Altogether, indexOf() is a convenient method for finding a character sequence buried in a text string without doing any coding for substring  It might be possible with an extremely complex lookbehind Regex (find the search substring, then look backward to find either the beginning of the string or the first letter after a period and one or more spaces not immediately prefaced by a known abbreviation, then forward to find the same) but I'm pretty sure any expression you create would fail some input designed specifically to test a weakness.

Counting the occurrences of a substring in a string using string.count , count() is an in-built function in Python, it is used to find the occurrences of a substring in a given string. It returns an integer value which is the  Given a input string and a substring. Find the frequency of occurrences of substring in given string. Examples: Input : man (pattern) dhimanman (string) Output : 2 Input : nn (pattern) Banana (String) Output : 0 Input : man (pattern) dhimanman (string) Output : 2 Input : aa (pattern) aaaaa (String) Output : 4.

Find all occurrences of a substring in Python, Python has string.find() and string.rfind() to get the index of a substring in string. I wonder, maybe there is something like string.find_all() which can return all  List<int> positions = new List<int>[]; should be List<int> positions = new List<int>(); – RickL Feb 15 '17 at 13:53. I believe this is a better solution. If the string you are trying to match is something like .s then the regex will return the incorrect number.

Python : Find occurrence count & all indices of a sub-string in , As, string.count() can not find the overlapping occurrences of a sub-string. So, let's Iterate over all the matches of substring using iterator of  Using indexOf() method The idea is to use the indexOf() method of the String class which returns the index within this string of the first occurrence of the specified substring, starting at the specified index. It returns -1 if there is no such occurrence. The trick is to start from the position where last found substring ends.

Comments
  • what should 'ttt'.find_all('tt') return?
  • it should return '0'. Of course, in perfect world there also has to be 'ttt'.rfind_all('tt'), which should return '1'
  • Seems like a duplicate of this stackoverflow.com/questions/3873361/…
  • hi, concerning this [m.start() for m in re.finditer('test', 'test test test test')], how can we look for test or text? Does it become much more complicated?
  • You want to look into regular expression in general : docs.python.org/2/howto/regex.html. The solution to your question will be : [m.start() for m in re.finditer('te[sx]t', 'text test text test')]
  • What will be the time complexity of using this method ?
  • @PranjalMittal. Upper or lower bound? Best, worst or average case?
  • @marcog what if the substring contains parentheses or other special characters?
  • To get overlapping matches, it should suffice to replace start += len(sub) with start += 1.
  • I believe your previous comment should be a postscript in your answer.
  • Your code does not work for finding substr: "ATAT" in "GATATATGCATATACTT"
  • See the comment I made in addition. That is an example of an overlapping match.