0

我试图找到具有特定关键字的文本的某个部分。我做到了,但我认为可能有更好的方法来做到这一点。

这是我的代码,

    /// <summary>
    /// Return String containing the search word
    /// </summary>
    /// <param name="strInput">Raw Input</param>
    /// <param name="searchWord">Search Word</param>
    /// <param name="minLength">Output Text Length</param>
    /// <returns></returns>
    public static String StringHavingSearchPattern(String strInput, String searchWord, Int32 minLength)
    {
        //considering the return string empty
        String rtn = String.Empty;
        //
        if (String.IsNullOrEmpty(strInput))
        {
            return rtn;
        }
        // consider the raw input as return;
        //rtn = strInput;
        int length = strInput.Length;

        //if the rawinput length is greater||equal than/to the min length
        if (length >= minLength)
        {
            int currentSearchIndex = -1;
            searchWord = String.Format(@"{0}", searchWord);
            //currentSearchIndex = strInput.IndexOf(searchWord,StringComparison.InvariantCultureIgnoreCase);
            Match m = Regex.Match(strInput, searchWord, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            if (m.Success)
            {
                currentSearchIndex = m.Index;
                if (currentSearchIndex >= 0)
                {
                    if (currentSearchIndex > 9)
                    {
                        if ((length - currentSearchIndex - 1) >= minLength)
                        {
                            rtn = strInput.Substring((currentSearchIndex - 9), minLength);
                        }
                        else
                        {
                            rtn = strInput.Substring((currentSearchIndex - 9), length - currentSearchIndex - 1);
                        }
                    }
                    else
                    {
                        rtn = strInput.Substring(0, minLength);
                    }
                }
                else
                {
                    rtn = strInput.Substring(0, minLength);
                }
            }
        }
        rtn = Regex.Replace(rtn, searchWord, String.Format("<span style='color:yellow;background-color:blue;'>{0}</span>", searchWord), RegexOptions.IgnoreCase | RegexOptions.Multiline);

        //rtn = rtn.Replace(searchWord, String.Format("<span style='color:yellow;background-color:blue;'>{0}</span>", searchWord));
        return rtn;
    }

寻找你的好建议来改进它。

4

1 回答 1

1

你的预感是对的;您可以使用一个简单的正则表达式来实现这一点:

Match m = Regex.Match(strInput, "(.{0," + minLength.ToString() + "}" + searchWord + ".*)", ...

例如,这将为searchWord“关键字”和minLength10 创建以下正则表达式,

(.{0,10}keyword.*)

这意味着,“最多捕获 10 个字符,后跟‘关键字’和文本的其余部分。” 然后,您可以抓取捕获的组:

m.Groups[1].Value

minLength请注意,在搜索词之前有少于字符的情况被覆盖。

于 2012-10-17T06:08:34.883 回答