So for a single word substring count in some text, I can use some_text.split().count(single_word_substring)
. How can I do that for a multi-word substring count in some text?
Examples:
text = 'he is going to school. abc is going to school. xyz is going to school.'
to_be_found = 'going to school'
count should be 3.
text = 'he is going to school. abc is going to school. xyz is going to school.'
to_be_found = 'going to'
count should be 3.
text = 'he is going to school. abc is going to school. xyz is going to school.'
to_be_found = 'go'
count should be 0.
text = 'he is going to school. abc-xyz is going to school. xyz is going to school.'
to_be_found = 'school'
count should be 3.
text = 'he is going to school. abc-xyz is going to school. xyz is going to school.'
to_be_found = 'abc-xyz'
count should be 1.
Assumption 1: Everything is lower-case.
Assumption 2: The text can contain anything.
Assumption 3: The to be found can contain anything too. For example, car with 4 passengers
, xyz & abc
, etc.
NOTE: REGEX based solutions are acceptable. I am just curious if it's possible without regex (nice to have and just for others who may be interested in this in future).
question from:
https://stackoverflow.com/questions/65928241/count-appearnce-of-multi-word-substring-in-some-text 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…