Python 正则表达式 : return a list of words containing a given substring

标签 python regex string

什么是基于正则表达式的函数 f,给定输入文本和字符串,返回文本中包含该字符串的所有单词。例如:

f("This is just a simple text to test some basic things", "si")

会返回:

["simple", "basic"]

(因为这两个词包含子串"si")

怎么做?

最佳答案

对于这样的事情我不会使用正则表达式,我会使用这样的东西:

def f(string, match):
    string_list = string.split()
    match_list = []
    for word in string_list:
        if match in word:
            match_list.append(word)
    return match_list

print f("This is just a simple text to test some basic things", "si")

关于Python 正则表达式 : return a list of words containing a given substring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29090167/

相关文章:

string - Erlang 如何从这个二进制文件 <<"a,b,c">> 中创建一个列表

Python简单字符串格式化错误

python - 获取 `Wall Clock'时间流逝的秒数

python - Django 类 View 和反向 URL

python - pandas - 用大写字母替换字符串中的一个字母

python - 在 python 中按 ","拆分

Java 正则表达式正向先行

string - MATLAB 字符串中的换行符

python - utcoffset 方法在 datetime 中的作用 - Python

python - 从 Python 3 中的值中删除 b'