Find out how many times a regex matches in a string in Python

后端 未结 8 1389
悲哀的现实
悲哀的现实 2020-11-27 06:09

Is there a way that I can find out how many matches of a regex are in a string in Python? For example, if I have the string \"It actually happened when it acted out of

8条回答
  •  庸人自扰
    2020-11-27 06:40

    I know this is a question about regex. I just thought I'd mention the count method for future reference if someone wants a non-regex solution.

    >>> s = "It actually happened when it acted out of turn."
    >>> s.count('t a')
    2
    

    Which return the number of non-overlapping occurrences of the substring

提交回复
热议问题