用c#中的regex以任何顺序查找字符串中的单词的最佳方法是什么?

时间:2022-12-11 01:34:40

So I have a string " I have a BIG RED CAR"

我有一根线"我有一辆红色的大车"

I want a user to be able to put in part of the string in any order. So they can write "CAR BIG RED" and the string will be found. I know I can do this by doing multiple IsMatch calls with a Regex string or I could use one string with anchors such as "^(?=.*CAR)(?=.*RED)(?=.*BIG).*$". I was wondering what would be the best option or is there an even better option?

我希望用户能够以任何顺序放入字符串的一部分。这样他们就可以写“汽车大红”字串就会被找到。我知道我可以做多个IsMatch调用与正则表达式字符串或我可以用一个字符串等锚”^(? =。*车)(? =。*红色)(? = *大)。*美元”。我想知道最好的选择是什么,或者有更好的选择吗?

Note: I am using C#, so any .net regex should work. All suggestions are welcome.

注意:我正在使用c#,所以任何。net regex都应该正常工作。所有的建议都是受欢迎的。

4 个解决方案

#1


13  

Stepping outside the realm of Regex, you could do something like this:

走出Regex的领域,你可以这样做:

string s = "I have a BIG RED CAR";

var searchTerms = "CAR BIG RED".Split(' ').ToList();
var hasTerms = searchTerms.All(s.Contains);

#2


4  

You could just check the words in the String against the words in the search String.

你可以将字符串中的单词与搜索字符串中的单词进行对比。

you could use Linq for this if you want

如果你愿意,你可以使用Linq

Linq:

List<string> items = new List<string>
{
    "I have a BIG RED CAR",
    "I have a BIG GREEN TRUCK", 
    "I have a BIG YELLOW BOAT"
};

string searchString = "CAR BIG RED";
string result = items.FirstOrDefault(x => searchString.Split(' ').All(s => x.Split(' ').Contains(s)));

Regex:

With RegEx you could concatenate all the individual words into a pipe delimited string and use with the \b operator to check for the whole words.

使用RegEx,您可以将所有单个单词连接到带分隔符的管道字符串中,并使用\b操作符检查整个单词。

string sentance = "I have a BIG RED CAR";
string searchString = "CAR BIG RED";

string regexPattern = string.Format(@"\b({0})\b", searchString.Replace(" ","|"));
if (Regex.IsMatch(sentance, regexPattern))
{
    // found all search words in string
}

However there couild be a nicer way to do this with RegEx, I only know the basics of RegEx

然而,用RegEx来做这件事可能有更好的方法,我只知道RegEx的基本知识

#3


0  

A simple idea without LINQ:

一个没有LINQ的简单想法:

var st = "CAR BIG RED".Split(' ');
var sm = "I have a BIG RED CAR";
bool res = true;
foreach (string s in st)
    res &= sm.Contains(s);

#4


0  

You can also use Array.FindAll method. See the example in the link.

还可以使用数组。FindAll方法。参见链接中的示例。

#1


13  

Stepping outside the realm of Regex, you could do something like this:

走出Regex的领域,你可以这样做:

string s = "I have a BIG RED CAR";

var searchTerms = "CAR BIG RED".Split(' ').ToList();
var hasTerms = searchTerms.All(s.Contains);

#2


4  

You could just check the words in the String against the words in the search String.

你可以将字符串中的单词与搜索字符串中的单词进行对比。

you could use Linq for this if you want

如果你愿意,你可以使用Linq

Linq:

List<string> items = new List<string>
{
    "I have a BIG RED CAR",
    "I have a BIG GREEN TRUCK", 
    "I have a BIG YELLOW BOAT"
};

string searchString = "CAR BIG RED";
string result = items.FirstOrDefault(x => searchString.Split(' ').All(s => x.Split(' ').Contains(s)));

Regex:

With RegEx you could concatenate all the individual words into a pipe delimited string and use with the \b operator to check for the whole words.

使用RegEx,您可以将所有单个单词连接到带分隔符的管道字符串中,并使用\b操作符检查整个单词。

string sentance = "I have a BIG RED CAR";
string searchString = "CAR BIG RED";

string regexPattern = string.Format(@"\b({0})\b", searchString.Replace(" ","|"));
if (Regex.IsMatch(sentance, regexPattern))
{
    // found all search words in string
}

However there couild be a nicer way to do this with RegEx, I only know the basics of RegEx

然而,用RegEx来做这件事可能有更好的方法,我只知道RegEx的基本知识

#3


0  

A simple idea without LINQ:

一个没有LINQ的简单想法:

var st = "CAR BIG RED".Split(' ');
var sm = "I have a BIG RED CAR";
bool res = true;
foreach (string s in st)
    res &= sm.Contains(s);

#4


0  

You can also use Array.FindAll method. See the example in the link.

还可以使用数组。FindAll方法。参见链接中的示例。