I wish to check if a string contains a all of the characters of a word given, for example:
我希望检查字符串是否包含给定单词的所有字符,例如:
var inputString = "this is just a simple text string";
And say I have the word:
并说我有这个词:
var word = "ts";
Now it should pick out the words that contains t and s:
现在它应该挑选出包含t和s的单词:
this just string
这只是字符串
This is what I am working on:
这就是我正在做的事情:
var names = Regex.Matches(inputString, @"\S+ts\S+",RegexOptions.IgnoreCase);
however this does not give me back the words I like. If I had like just a character like t, it would give me back all of the words that contains t. If I had st instead of ts, it would give me back the word just.
但这并没有让我回复我喜欢的词。如果我只喜欢像t这样的角色,那么它会给我所有包含t的单词。如果我有st而不是ts,它会让我回复这个词。
Any idea of how this can work ?
知道这是如何工作的吗?
4 个解决方案
#1
10
Here is a LINQ solution which is easy on the eyes more natural than regex.
这是一个LINQ解决方案,比正则表达式更容易让人眼前一亮。
var testString = "this is just a simple text string";
string[] words = testString.Split(' ');
var result = words.Where(w => "ts".All(w.Contains));
The result is:
结果是:
this
just
string这只是字符串
#2
7
You can use LINQ's Enumerable.All
:
您可以使用LINQ的Enumerable.All:
var input = "this is just a simple text string";
var token = "ts";
var results = input.Split().Where(str => token.All(c => str.Contains(c))).ToList();
foreach (var res in results)
Console.WriteLine(res);
Output:
输出:
// this
// just
// string
#3
6
You can use this pattern.
您可以使用此模式。
(?=[^ ]*t)(?=[^ ]*s)[^ ]+
You can make regex dynamically.
你可以动态制作正则表达式。
var inputString = "this is just a simple text string";
var word = "ts";
string pattern = "(?=[^ ]*{0})";
string regpattern = string.Join("" , word.Select(x => string.Format(pattern, x))) + "[^ ]+";
var wineNames = Regex.Matches(inputString, regpattern ,RegexOptions.IgnoreCase);
#4
3
Option without LINQ and Regex (just for fun):
没有LINQ和Regex的选项(只是为了好玩):
string input = "this is just a simple text string";
char[] chars = { 't', 's' };
var array = input.Split();
List<string> result = new List<string>();
foreach(var word in array)
{
bool isValid = true;
foreach (var c in chars)
{
if (!word.Contains(c))
{
isValid = false;
break;
}
}
if(isValid) result.Add(word);
}
#1
10
Here is a LINQ solution which is easy on the eyes more natural than regex.
这是一个LINQ解决方案,比正则表达式更容易让人眼前一亮。
var testString = "this is just a simple text string";
string[] words = testString.Split(' ');
var result = words.Where(w => "ts".All(w.Contains));
The result is:
结果是:
this
just
string这只是字符串
#2
7
You can use LINQ's Enumerable.All
:
您可以使用LINQ的Enumerable.All:
var input = "this is just a simple text string";
var token = "ts";
var results = input.Split().Where(str => token.All(c => str.Contains(c))).ToList();
foreach (var res in results)
Console.WriteLine(res);
Output:
输出:
// this
// just
// string
#3
6
You can use this pattern.
您可以使用此模式。
(?=[^ ]*t)(?=[^ ]*s)[^ ]+
You can make regex dynamically.
你可以动态制作正则表达式。
var inputString = "this is just a simple text string";
var word = "ts";
string pattern = "(?=[^ ]*{0})";
string regpattern = string.Join("" , word.Select(x => string.Format(pattern, x))) + "[^ ]+";
var wineNames = Regex.Matches(inputString, regpattern ,RegexOptions.IgnoreCase);
#4
3
Option without LINQ and Regex (just for fun):
没有LINQ和Regex的选项(只是为了好玩):
string input = "this is just a simple text string";
char[] chars = { 't', 's' };
var array = input.Split();
List<string> result = new List<string>();
foreach(var word in array)
{
bool isValid = true;
foreach (var c in chars)
{
if (!word.Contains(c))
{
isValid = false;
break;
}
}
if(isValid) result.Add(word);
}