[英]How to extract phrases and then words in a string of text?
我有一個搜索方法,該方法接受用戶輸入的字符串,在每個空格字符處將其分割,然后根據分隔的術語列表查找匹配項:
string[] terms = searchTerms.ToLower().Trim().Split( ' ' );
現在,我又有了進一步的要求:能夠通過雙引號分隔符la Google搜索短語。 因此,如果提供的搜索詞是:
“一行”文字
搜索將匹配出現的“一行”和“文本”,而不是四個單獨的術語(在搜索之前,也需要刪除左引號和右引號)。
如何在C#中實現呢? 我認為正則表達式是解決問題的一種方法,但是並沒有花太多時間研究它們,因此不知道它們是否是最佳解決方案。
如果您需要更多信息,請詢問。 先謝謝您的幫助。
這是一個正則表達式模式,它將返回名為“ term
”的組中的匹配term
:
("(?<term>[^"]+)"\s*|(?<term>[^ ]+)\s*)+
因此對於輸入:
"a line" of text
“ term
”組標識的輸出項目為:
a line
of
text
正則表達式絕對是必經之路...
您應該檢查此MSDN鏈接以獲取有關Regex類的一些信息: http : //msdn.microsoft.com/zh-cn/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;
使用正則表達式...
string textToSearchIn =“”一行“ text”;
字符串結果= Regex.Match(textToSearchIn,“(?<=”)。*?(?=“)”)。Value;
或者如果多於一個,將其放入比賽集合中...
MatchCollection allPhrases = Regex.Matches(textToSearchIn,“(?<=”)。*?(?=“)”);
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);
}
}
}
}
試試這個,它將返回一個文本數組。 例如:{“一行”文字“記事本”}:
string textToSearch = "\"a line of\" text \" notepad\"";
MatchCollection allPhrases = Regex.Matches(textToSearch, "(?<=\").*?(?=\")");
var RegArray = allPhrases.Cast<Match>().ToArray();
輸出:{“一行”,“文本”,“記事本”}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.