第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從大字符串中搜索關(guān)鍵字中獲取“N”個前一個和下一個單詞

從大字符串中搜索關(guān)鍵字中獲取“N”個前一個和下一個單詞

C#
慕俠2389804 2023-08-20 15:59:40
我正在尋找可以從字符串中搜索的關(guān)鍵字中獲取 -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;輸出將是:例如,一個用例是將代碼映射到某些目前,我正在研究文本挖掘主題,其中我必須提取文件并從提取的字符串中搜索特定關(guān)鍵字及其句子。以前,每當(dāng)我獲得所需的關(guān)鍵字時,我都會從字符串中獲取第一句話。但現(xiàn)在要求已按上面的方式更改,這是代碼片段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]);    }}點網(wǎng)小提琴這是我得到的輸出輸出:- 一種用例是將代碼映射到某些數(shù)據(jù)源或第三方 API,其中名稱用作鍵這給了我第一句話,我得到了所需的關(guān)鍵字,任何建議我應(yīng)該做什么改變來獲得新的所需輸出。
查看完整描述

2 回答

?
慕標(biāo)5832272

TA貢獻(xiàn)1966條經(jīng)驗 獲得超4個贊

怎么樣:1)將其拆分為單詞 2)找到您的索引3)從找到索引之前keyword開始獲取一系列單詞5


using System;

using System.Linq;


namespace Foo

{

    class Program

    {

        static void Main(string[] args)

        {

             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 words = sentence.Split(' ').ToArray(); // split into words

            int index = Array.FindIndex(words, w => w.Equals(keyword)); // find the index within

            // take 11 words from 5 before the index of your keyword

            var r = Enumerable

                .Range(index - 5, 11)

                .Select(i => words[i]);

            var result = string.Join(' ', r);


            Console.WriteLine("Output:- {0} ", result);

            Console.ReadKey();

        }

    }

}

這會產(chǎn)生您想要的輸出,但不會處理:

  1. 多場比賽

  2. 匹配不同的案例

  3. IndexOutOfRangeException獲得所需單詞時的可能性


查看完整回答
反對 回復(fù) 2023-08-20
?
慕的地6264312

TA貢獻(xiàn)1817條經(jīng)驗 獲得超6個贊

using System.Linq;

using System.Text.RegularExpressions;

using System;


public class Program

{

    public static void Main()

    {

            var sentence = "case is for instance doooo 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.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";


            int wordFreq = 2;

            var words = sentence.Split(' ').ToArray(); // split into words

            int foundndex = Array.FindIndex(words, w => w.Equals(keyword)); // find the index within

                                                                            // take wordFreq words from wordFreq before the index of your keyword

            var wordsArray = Enumerable

                    .Range((foundndex - wordFreq) > 0 ? (foundndex - wordFreq) : 0, (wordFreq*2+1 > (words.Length)-1) ? (words.Length)-1 : wordFreq*2+1 )

                    .Select(i => words[i]).ToArray();            


            var outPut = string.Join(" ", wordsArray);


            Console.WriteLine("Output:- {0} ",outPut);              

    }

}

希望我處理了所有可能的異常!

點網(wǎng)小提琴


查看完整回答
反對 回復(fù) 2023-08-20
  • 2 回答
  • 0 關(guān)注
  • 162 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號