簡體   English   中英

Python 正則表達式:返回包含給定子字符串的單詞列表

[英]Python regexes: return a list of words containing a given substring

基於正則表達式的函數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")
import re

def func(s, pat):
    pat = r'\b\S*%s\S*\b' % re.escape(pat) 
    return re.findall(pat, s)


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

你需要這個 。 \\b將通過在單詞邊界處切割來僅取出單詞。 \\S不會選擇任何space

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM