2

ユーザーが入力した文字列を受け取り、それを各スペース文字で分割してから、分離された用語のリストに基づいて一致するものを検索する検索方法があります。

string[] terms = searchTerms.ToLower().Trim().Split( ' ' );

今、私はさらなる要件を与えられました:グーグルの二重引用符区切り文字を介してフレーズを検索できるようにすること。したがって、提供された検索用語が次の場合:

「一行の」テキスト

検索は、4つの別々の用語ではなく、「行」と「テキスト」の出現に一致します[検索する前に、開始と終了の二重引用符も削除する必要があります]。

どうすればC#でこれを達成できますか?正規表現が道のりだと思いますが、あまり手を出していないので、それらが最善の解決策であるかどうかはわかりません。

さらに情報が必要な場合は、お問い合わせください。助けてくれてありがとう。

4

6 に答える 6

2

termこれは、' 'という名前のグループで一致を返す正規表現パターンです。

("(?<term>[^"]+)"\s*|(?<term>[^ ]+)\s*)+

したがって、入力については:

"a line" of text

term' 'グループで識別される出力項目は次のようになります。

a line
of
text
于 2009-09-09T15:45:22.517 に答える
1

正規表現は間違いなく行く方法です...

Regexクラスに関する情報については、次のMSDNリンクを確認する必要があります:http: //msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx

正規表現の構文を学ぶための優れたリンクは次のとおりです 。http ://www.radsoftware.com.au/articles/regexlearnsyntax.aspx

次に、いくつかのコード例を追加するために、次の行に沿って何かを行うことができます。

string searchString = "a line of";

Match m = Regex.Match(textToSearch, searchString);

または、文字列に一致が含まれているかどうかを確認したいだけの場合:

bool success = Regex.Match(textToSearch, searchString).Success;
于 2009-09-09T15:43:21.227 に答える
1

ここで正規表現ビルダーを使用します

http://gskinner.com/RegExr/

正規表現を必要な表示方法に操作できるようになります

于 2009-09-09T15:47:41.233 に答える
1

Regexsを使用してください...。

string textToSearchIn = "" a line of "text";
string result = Regex.Match(textToSearchIn、 "(?<=")。*?(?= ")")。Value;

または、複数ある場合は、これをマッチコレクションに入れます...

MatchCollection allPhrases = Regex.Matches(textToSearchIn、 "(?<=")。*?(?= ")");

于 2009-09-09T15:48:46.050 に答える
0

Knuth-Morris-Pratt (KMPアルゴリズム)は、文字列内の部分文字列(技術的には文字列ではなくバイト配列)を見つけるための最速のアルゴリズムとして認識されています。

using System.Collections.Generic;

namespace KMPSearch
{
    public class KMPSearch
    {
        public static int NORESULT = -1;

        private string _needle;
        private string _haystack;
        private int[] _jumpTable;

        public KMPSearch(string haystack, string needle)
        {
            Haystack = haystack;
            Needle = needle;
        }

        public void ComputeJumpTable()
        {
            //Fix if we are looking for just one character...
            if (Needle.Length == 1)
            {
                JumpTable = new int[1] { -1 };
            }
            else
            {
                int needleLength = Needle.Length;
                int i = 2;
                int k = 0;

                JumpTable = new int[needleLength];
                JumpTable[0] = -1;
                JumpTable[1] = 0;

                while (i <= needleLength)
                {
                    if (i == needleLength)
                    {
                        JumpTable[needleLength - 1] = k;
                    }
                    else if (Needle[k] == Needle[i])
                    {
                        k++;
                        JumpTable[i] = k;
                    }
                    else if (k > 0)
                    {
                        JumpTable[i - 1] = k;
                        k = 0;
                    }

                    i++;
                }
            }
        }

        public int[] MatchAll()
        {
            List<int> matches = new List<int>();
            int offset = 0;
            int needleLength = Needle.Length;
            int m = Match(offset);

            while (m != NORESULT)
            {
                matches.Add(m);
                offset = m + needleLength;
                m = Match(offset);
            }

            return matches.ToArray();
        }

        public int Match()
        {
            return Match(0);
        }

        public int Match(int offset)
        {
            ComputeJumpTable();

            int haystackLength = Haystack.Length;
            int needleLength = Needle.Length;

            if ((offset >= haystackLength) || (needleLength > ( haystackLength - offset))) 
                return NORESULT;

            int haystackIndex = offset;
            int needleIndex = 0;

            while (haystackIndex < haystackLength)
            {
                if (needleIndex >= needleLength)
                    return haystackIndex;

                if (haystackIndex + needleIndex >= haystackLength)
                    return NORESULT;

                if (Haystack[haystackIndex + needleIndex] == Needle[needleIndex])
                {
                    needleIndex++;
                } 
                    else
                {
                    //Naive solution
                    haystackIndex += needleIndex;

                    //Go back
                    if (needleIndex > 1)
                    {
                        //Index of the last matching character is needleIndex - 1!
                        haystackIndex -= JumpTable[needleIndex - 1];
                        needleIndex = JumpTable[needleIndex - 1];
                    }
                    else
                        haystackIndex -= JumpTable[needleIndex];


                }
            }

            return NORESULT;
        }

        public string Needle
        {
            get { return _needle; }
            set { _needle = value; }
        }

        public string Haystack
        {
            get { return _haystack; }
            set { _haystack = value; }
        }

        public int[] JumpTable
        {
            get { return _jumpTable; }
            set { _jumpTable = value; }
        }
    }
}

使用法 :-

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace KMPSearch
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage: " + Environment.GetCommandLineArgs()[0] + " haystack needle");
            }
            else
            {
                KMPSearch search = new KMPSearch(args[0], args[1]);
                int[] matches = search.MatchAll();
                foreach (int i in matches)
                    Console.WriteLine("Match found at position " + i+1);
            }
        }

    }
}
于 2009-09-09T15:54:12.903 に答える
0

これを試してください。テキストの配列が返されます。例:{「1行の」テキスト「メモ帳」}:

string textToSearch = "\"a line of\" text \" notepad\"";

MatchCollection allPhrases = Regex.Matches(textToSearch, "(?<=\").*?(?=\")");

var RegArray = allPhrases.Cast<Match>().ToArray();

出力:{"行"、 "テキスト"、"メモ帳"}

于 2016-01-04T12:52:17.020 に答える