<legend id='aJ7TE'><style id='aJ7TE'><dir id='aJ7TE'><q id='aJ7TE'></q></dir></style></legend>

<tfoot id='aJ7TE'></tfoot>
    • <bdo id='aJ7TE'></bdo><ul id='aJ7TE'></ul>
    <i id='aJ7TE'><tr id='aJ7TE'><dt id='aJ7TE'><q id='aJ7TE'><span id='aJ7TE'><b id='aJ7TE'><form id='aJ7TE'><ins id='aJ7TE'></ins><ul id='aJ7TE'></ul><sub id='aJ7TE'></sub></form><legend id='aJ7TE'></legend><bdo id='aJ7TE'><pre id='aJ7TE'><center id='aJ7TE'></center></pre></bdo></b><th id='aJ7TE'></th></span></q></dt></tr></i><div id='aJ7TE'><tfoot id='aJ7TE'></tfoot><dl id='aJ7TE'><fieldset id='aJ7TE'></fieldset></dl></div>

    <small id='aJ7TE'></small><noframes id='aJ7TE'>

    1. 如何找到所有出现的子字符串?

      时间:2024-04-21
    2. <i id='RdGpE'><tr id='RdGpE'><dt id='RdGpE'><q id='RdGpE'><span id='RdGpE'><b id='RdGpE'><form id='RdGpE'><ins id='RdGpE'></ins><ul id='RdGpE'></ul><sub id='RdGpE'></sub></form><legend id='RdGpE'></legend><bdo id='RdGpE'><pre id='RdGpE'><center id='RdGpE'></center></pre></bdo></b><th id='RdGpE'></th></span></q></dt></tr></i><div id='RdGpE'><tfoot id='RdGpE'></tfoot><dl id='RdGpE'><fieldset id='RdGpE'></fieldset></dl></div>

      <small id='RdGpE'></small><noframes id='RdGpE'>

    3. <legend id='RdGpE'><style id='RdGpE'><dir id='RdGpE'><q id='RdGpE'></q></dir></style></legend>

        <tfoot id='RdGpE'></tfoot>

            <tbody id='RdGpE'></tbody>
            <bdo id='RdGpE'></bdo><ul id='RdGpE'></ul>

                本文介绍了如何找到所有出现的子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                Python 有 string.find()string.rfind() 来获取字符串中子串的索引.

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

                我想知道是否有类似 string.find_all() 的东西可以返回所有找到的索引(不仅是从头开始的第一个索引,也不是从最后的第一个索引).

                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).

                例如:

                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]
                

                如果您想查找重叠匹配,lookahead 会这样做:

                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 返回一个generator,所以你可以把上面的 [] 改成 () 来获得一个生成器而不是一个列表,如果你只迭代一次结果,这将更有效.

                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 中做了什么?- super().__init__() 和显式超类 __init__( 下一篇:“和"怎么做?和“或"以非布尔值行事?

                相关文章

                  <bdo id='lPs0i'></bdo><ul id='lPs0i'></ul>

                <tfoot id='lPs0i'></tfoot>
                <i id='lPs0i'><tr id='lPs0i'><dt id='lPs0i'><q id='lPs0i'><span id='lPs0i'><b id='lPs0i'><form id='lPs0i'><ins id='lPs0i'></ins><ul id='lPs0i'></ul><sub id='lPs0i'></sub></form><legend id='lPs0i'></legend><bdo id='lPs0i'><pre id='lPs0i'><center id='lPs0i'></center></pre></bdo></b><th id='lPs0i'></th></span></q></dt></tr></i><div id='lPs0i'><tfoot id='lPs0i'></tfoot><dl id='lPs0i'><fieldset id='lPs0i'></fieldset></dl></div>
              • <small id='lPs0i'></small><noframes id='lPs0i'>

                <legend id='lPs0i'><style id='lPs0i'><dir id='lPs0i'><q id='lPs0i'></q></dir></style></legend>