C# 在具有特定条件的字符串中搜索子字符串
我想弄清楚,在其他字符串中搜索子字符串以获得相等的内容输出的正确方法是什么,例如:C# 在具有特定条件的字符串中搜索子字符串,c#,list,C#,List,我想弄清楚,在其他字符串中搜索子字符串以获得相等的内容输出的正确方法是什么,例如: hello how are you 输入为true时: hey hello how are you ok how are you are you 这是错误的: you how you are ok you howareyou howok how you hey hello 我希望字符串中包含相等的短语或短语的一部分,而不是单个单词或其他序列中的单词。这样一来,对于所有具有(aList.Any(input
hello how are you
输入为true时:
hey hello how are you ok
how are you
are you
这是错误的:
you
how you are ok you
howareyou
howok
how you
hey hello
我希望字符串中包含相等的短语或短语的一部分,而不是单个单词或其他序列中的单词。这样一来,对于所有具有(aList.Any(input.Contains))
的对象都为真,对于所有具有(aList.Contains(input))
的对象都为假:
List aList=新列表(){
“你好,你好吗”,
“你好吗”,
“是你吗”,
“你”,
“你还好吗你”,
“你好”,
“你好”,
“你好”,
“你好”};
string input=“你好”;
foreach(列表中的字符串a)
{
如果(a.Any(input.Contains))
{
Console.WriteLine(a+“-true”);
}
其他的
{
控制台。写线(a+“-假”);
}
}
Console.WriteLine(“\n\r”);
foreach(列表中的字符串a)
{
如果(a.包含(输入))
{
Console.WriteLine(a+“-true”);
}
其他的
{
控制台。写线(a+“-假”);
}
}
这应该暂时有效,因为您没有指定任何其他内容。至少有2个单词出现,以返回true
string [] array = input.Split(' ');
foreach(string a in list)
{
bool yes = false;
for(int i = 0; i < array.Length-1; ++i ){
string test = array[i] + " " + array[i+1];
if(a.Contains(test)){
yes = true;
}
}
Console.WriteLine(yes);
}
string[]数组=input.Split(“”);
foreach(列表中的字符串a)
{
布尔是=假;
对于(int i=0;i
我想出了这个解决方案:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
List<string> aList = new List<string>()
{"hey hello how are you ok", "how are you", "are you", "you", "how you are ok you", "howareyou", "howok", "how you", "hey hello"};
var input = "hello how are you";
// build matcher
string[] chunks = input.Split(' ');
string matcher = "";
for (int i = chunks.Length, j = 0; i > 1; i--, j++){
var matcherPart = new string [i];
Array.Copy(chunks, j, matcherPart, 0, i);
matcher += "("+String.Join(@"+\s+", matcherPart) + ")";
}
matcher = matcher.Replace(")(", ")|(");
// Console.WriteLine(matcher);
//(hello+\s+how+\s+are+\s+you)|(how+\s+are+\s+you)|(are+\s+you)";
foreach (string a in aList)
{
Regex r = new Regex(matcher, RegexOptions.IgnoreCase);
Match m = r.Match(a);
Group g = m.Groups[0];
Console.WriteLine(a + " - " + (g.Captures.Count > 0));
}
/*
hey hello how are you ok - True
how are you - True
are you - True
you - False
how you are ok you - False
howareyou - False
howok - False
how you - False
hey hello - False
*/
}
}
使用系统;
使用System.Collections.Generic;
使用System.Text.RegularExpressions;
公共课程
{
公共静态void Main()
{
列表列表=新列表()
{“你好,你好吗”,“你好吗”,“你好吗”,“你”,“你好吗”,“你好吗”,“你好吗”,“你好吗”,“你好吗”,“你好吗”,“你好吗”,“你好吗”,“你好吗”,“你好你好吗”};
var input=“你好”;
//构建匹配器
string[]chunks=input.Split(“”);
字符串匹配器=”;
for(inti=chunks.Length,j=0;i>1;i--,j++){
var matcherPart=新字符串[i];
复制(块,j,匹配部分,0,i);
matcher+=“(“+String.Join(+“+\s+”,matcherPart)+”);
}
匹配器=匹配器。替换(“)(“,”)|(”;
//控制台写入线(匹配器);
//(你好+\s+how+\s+are+\s+you);(how+\s+are+\s+you);(are+\s+you);
foreach(列表中的字符串a)
{
Regex r=新的Regex(matcher,RegexOptions.IgnoreCase);
匹配m=r.匹配(a);
g组=m组[0];
Console.WriteLine(a+“-”+(g.Captures.Count>0));
}
/*
嗨,你好,你还好吗
你说的对吗
你是真的吗
你错了
你还好吗?你错了
你好-错
howok-错误
你怎么会错
嗨,你好,False
*/
}
}
构建匹配器部分使用可能的组合创建regexp,即此字符串
abcd
正在转换为:(a+b+c+d)|(b+c+d)|(c+d)
。有了它,您可以轻松地循环查看列表值并应用regexp。g.Captures.Count
告诉您列表中的项目是否与您的模式匹配。我将此问题分为两个步骤
1) 按”
(空格)分割输入,并收集所有可能匹配的子短语
:
string input = "hello how are you";
string[] inputParts = input.Split(' ');
List<string> subphrases = new List<string>();
for (int i = 0; i < inputParts.Length-1; i++)
{
subphrases.Add(string.Join(" ", inputParts.Skip(i)));
}
代码中的顺序与我的句子不同。Any
将为aList
中的每个corpusItem
返回true
,其中至少包含子短语
集合中的一个元素。(我希望这更清楚;)
要获得FALSE
匹配的列表,只需对任何条件求反:
List<string> all_FALSE_Matches = aList.Where(
corpusItem => !subphrases.Any(sub => corpusItem.Contains(sub))).ToList();
列出所有\u FALSE\u匹配项=列表。其中(
corpusItem=>!subphrases.Any(sub=>corpusItem.Contains(sub))).ToList();
Hi,给列表一个真实的名字。如果你能用语言描述什么是真的,什么是假的结果,那么它是可能的。否则,你必须列出并比较所有的真可能性和假可能性。你为什么要为“是你”返回true
,而为“你”返回false
“?@Andy G hello,我希望字符串中包含相等的短语或短语的一部分,而不是单个单词或另一个单词中的单词sequence@Greggz你好,是的,这句话的一部分,但是没有一个词解释一下为什么这解决了问题会真正提高你的回答的质量。构建匹配器
部分创建了带有可能组合的regexp,即这个字符串abcd
正在转换为:(a)|(b)|(c)|(d)
。这样,您就可以轻松地循环列表值并应用regexp。g.Captures.Count
告诉您列表中的项目是否与您的模式匹配。听起来不错。你为什么不把它加到答案上呢。我们都在这里学习,解释是关键。你会在你的答案上看到它。干杯,伙计
List<string> all_TRUE_Matches = aList.Where(
corpusItem => subphrases.Any(sub => corpusItem.Contains(sub))).ToList();
List<string> all_FALSE_Matches = aList.Where(
corpusItem => !subphrases.Any(sub => corpusItem.Contains(sub))).ToList();