Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python正则表达式:返回包含给定子字符串的单词列表_Python_Regex_String - Fatal编程技术网

Python正则表达式:返回包含给定子字符串的单词列表

Python正则表达式:返回包含给定子字符串的单词列表,python,regex,string,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()

什么是基于正则表达式的函数
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")

我不认为没有比我的方法更好的方法了,但我认为:

import re

def f(s, pat):
    pat = r'(\w*%s\w*)' % pat       # Not thrilled about this line
    return re.findall(pat, s)


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

['simple', 'basic']

这是我试图解决的问题。我将输入字符串按“”拆分,然后尝试将每个单词与模式匹配。如果找到匹配项,则将该单词添加到结果集中

import re

def f(str, pat):
    matches = list()
    str_list = str.split(' ');

    for word in str_list:
        regex = r'' + re.escape(word)
        match = re.search(regex, word)
        if match:
            matches.append(word)
    return matches

print f("This is just a simple text to test some basic things", "si")
重新导入
def func(s,pat):
pat=r'\b\S*%S\S*\b'%r.escape(pat)
返回findall(帕特,s)
print func(“这只是一个测试一些基本东西的简单文本”,“si”)

您需要它。
\b
将通过在单词边界处剪切只取出单词。
\S
不会选择任何
空格

如果字符串有特殊字符该怎么办?它应该为“这是一些基本事物的简单基本测试”或“这是一些基本事物的简单基本测试”返回什么?
过滤器((lambda word:word中的子字符串),text.split())
?为什么需要正则表达式?
[x代表words中的x.split()(如果在x中搜索字符串)]
应该这样做。