我正在寻找解决方案,我可以从字符串中的搜索关键字中获取 -nth +nth 个单词
前任。
string searchString= "For several years I’ve had a little “utility” function that I’ve used in several projects that I use to convert property names into strings. One use case is for instance when mapping code to some data source or third party API that where the names are used as keys...";
string keywordToSearch="instance";
int wordsToFetch=5;
输出将是:一个用例是例如将代码映射到某个
目前,我正在研究文本挖掘主题,我必须提取文件并从提取的字符串中搜索特定的关键字+它的句子。以前,每当我获得所需的关键字时,我都会从字符串中获取第一句话。但是现在按照上面的要求改变了这里是代码片段
using System.Linq;
using System.Text.RegularExpressions;
using System;
public class Program
{
public static void Main()
{
var sentence = "For several years I’ve had a little “utility” function that I’ve used in several projects that I use to convert property names into strings. One use case is for instance when mapping code to some data source or third party API that where the names are used as keys. The method uses “static reflection”, or rather it parses the expression tree from a lambda expression, to figure out the name of a property that the lambda expression returns the value of.Look, good against remotes is one thing, good against the living, that’s something else.";
var keyword = "instance";
var keyToSearch = new Regex("[^.!?;]*(" + keyword + ")[^.!?;]*");
var m = keyToSearch.Matches(sentence);
var result1 = Enumerable.Range(0, m.Count).Select(index => m[index].Value).ToList();
Console.WriteLine("Output:- {0} ",result1[0]);
}
}
这是我得到的输出
输出:- 一个用例是例如将代码映射到某些数据源或第三方 API,其中名称用作键
这给了我获得所需关键字的第一句话,任何建议我应该做哪些更改以获得新的所需输出。