Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

python - count appearnce of multi-word substring in some text

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a working solution using regex:

import re

def occurrences(text,to_be_found):
    return len(re.findall(rf'W{to_be_found}W', text))

The capital W in regex is for non-word characters, which covers spaces and other punctuation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...