这是一个有趣的问题,假设您有一个包含3个句子的列表,例如:
比尔猫已经了 比尔有一只猫Cat有比尔
我如何使用.Contains()或任何其他方法来检查列表中的句子是否包含特定顺序的单词,算法如下所示:
1)通过foreach循环运行句子列表
2)检查句子是否包含此顺序中的单词=>比尔+有猫+
3)返回该句
所以每个其他句子都返回false,因为单词的顺序不同。伙计们,有关实施这个的想法吗? :)
答案 0 :(得分:2)
List<string> ss = new List<string>();
ss.Add("Bill had cat");
string inputstring = "In 2012, Bill had a cat";
foreach (string s in ss)
{
string[] split = s.Split(' ');
string regex = ".*";
for(int a = 0; a<split.Length;a++)
regex += split[a]+".*";
Match temp = Regex.Match(inputstring, regex);
if (temp.Success)
{
MessageBox.Show("String \"" + inputstring + "\" matches");
}
}
尝试这是否有效,这是区分大小写的,所以考虑到这一点
答案 1 :(得分:2)
尝试下面的解决方案它有效。
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("Bill cat had");
list.Add("Bill had a cat");
list.Add("Bill had cat");
list.Add("Cat had Bill");
Regex rex = new Regex(@"((Bill)).*((had)).*((cat))");
foreach (string str in list)
{
if (rex.IsMatch(str))
{
Console.WriteLine(str);
}
}
Console.ReadLine();
}
}
答案 2 :(得分:1)
这对我有用。
class Program
{
static List<string> sentences = new List<string>();
static List<string> pattern = new List<string>();
static List<string> results = new List<string>();
static void Main(string[] args)
{
//sentences are in order
sentences.Add("Bill cat had");
sentences.Add("Bill had a cat");
sentences.Add("Cat had Bill");
sentences.Add("Bill had cats");
//patters are in order
pattern.Add("Bill");
pattern.Add("had");
pattern.Add("cat");
// call the searchString method
results = searchString(sentences, pattern);
foreach (string res in results)
{
Console.WriteLine(res);
}
Console.Read(); // just keep program running when debugged
}
static List<string> searchString(List<string> sentences, List<string> patterns)
{
bool result = false;
List<string> resultLIst = new List<string>();
foreach (string sen in sentences)
{
int j = 0;
foreach (string pat in pattern)
{
if (sen.Contains(pat))
{
if (j <= sen.IndexOf(pat))
{
result = true;
}
else
{
result = false;
break;
}
j = sen.IndexOf(pat);
}
else
{
result = false;
break;
}
}
if (result)
resultLIst.Add(sen); // get the matching sentence
}
return resultLIst; // return matchin sentences
}
}
答案 3 :(得分:1)
同时检查此答案
protected void Page_Load(object sender, EventArgs e)
{
string message = "";
string[] list = new string[] { "Bill cat had", "Bill had a cat", "Cat had Bill" };
foreach (var item in list)
{
string[] splitString = item.Trim().Split(' ');
int i = 0; bool IsValid = true;
int count = 0;
foreach (var sindividual in splitString)
{
int j = CheckMatched(sindividual);
if (j != 0)
{
if (j > i)
{
i = j;
count++;
}
else
{
IsValid = false;
}
}
}
if (count >= 3 && IsValid)
{
message += item + " " + "yes it has t proper order \n";
}
else
{
message += item + " " + "Doesnt have proper order \n";
}
}
lblMessage.Text = message;
}
int CheckMatched(string sStringtoCheck)
{
sStringtoCheck = sStringtoCheck.Trim().ToLower();
if (sStringtoCheck.Contains("bill"))
{
return 1;
}
else if (sStringtoCheck.Contains("had"))
{
return 2;
}
else if (sStringtoCheck.Contains("cat"))
{
return 3;
}
else return 0;
}
这也行得很好..但是很小的