简体   繁体   English

如何匹配RegEx中用空格分隔的所有单词?

[英]How to match all words separated by spaces in RegEx?

I am studying regex, but still find hard to learn. 我正在研究正则表达式,但仍然难以学习。
So my problem is this, I have given a set of keywords: 所以我的问题是这个,我给出了一组关键字:

The quick brown fox 快速的棕色狐狸

where I have to find in bunch of sentences like: 在那里我必须找到一堆句子,如:

the Brown SexyFox Jumps soQuickly in the backyard... Brown SexyFox在后院快速跳跃......

If there is any match with these words (not Casesensitive): 如果与这些单词匹配(不是Casesensitive):

The, the, brown, Brown, fox, Fox, quick, Quick ,棕色,棕色,狐狸,狐狸,快速,快速

Then I can say that return value is true 然后我可以说返回值是true

How to do it in regex? 如何在正则表达式中执行此操作? I was thinking to split the words and put in Array and use loop and find them using .Contains(...) but I know that is not ideal. 我正在考虑拆分单词并放入数组并使用循环并使用.Contains(...)找到它们但我知道这并不理想。

Actually I have another concern. 其实我有另一个问题。 But I'm afraid to post it as a new question. 但是我害怕把它作为一个新问题发布。
So my second question is, how does regex read the pattern ? 所以我的第二个问题是, 正则表达式如何读取模式 What are the priorities and least priorities? 什么是优先事项和最不重要的事项?
Anyway please help me with my problem. 无论如何,请帮助我解决我的问题。

EDIT 编辑

Sorry for the late response, but the solution of @PatrikW seems not to work. 对于迟到的回复感到抱歉,但@ PatrikW的解决方案似乎不起作用。
I have static class: 我有静态类:

    public static bool ValidateRegex(string value, string regex)
    {
        value += ""; // Fail safe for null
        Regex obj = new Regex(regex, RegexOptions.IgnoreCase);
        if (value.Trim() == "")
            return false;
        else
        {
            return obj.IsMatch(value);
        }
    }  

Construct regex pattern: 构造正则表达式模式:

keyword = "maria";
            string regexPattern = "(?<=\b)(";
            string Or = string.Empty;

            foreach (string item in keyword.Split(new char[] { ' ', ',', '.' }, StringSplitOptions.RemoveEmptyEntries).ToList())
            {
                regexPattern += Or + "(" + item + ")";
                Or = "|";
            }

            regexPattern += ")(?=\b)";  

Data information: 数据信息:

List<Friend> useritems = null;
useritems = ((List<Friend>)SessonHandler.Data.FriendList).Where(i =>
    Utility.ValidateRegex(i.LastName, regexPattern) ||
    Utility.ValidateRegex(i.FirstName, regexPattern) ||
    Utility.ValidateRegex(i.MiddleName, regexPattern)).ToList();

//regexPattern = "(?<=\b)((maria))(?=\b)"
//LastName = "MARIA CALIBRI"
//FirstName = "ALICE"
//MiddleName = null  

May be I did something wrong with the code. 可能是我对代码做错了。 Please help. 请帮忙。

EDIT 2 编辑2
I forgot the @ sign. 我忘记了@符号。 This must work now: 这必须现在工作:

string regexPattern = @"(?<=\b)(";
.
.
.
regexPattern += @")(?=\b)";  

The answer below is correct. 以下答案是正确的。

What Felice showed is the more dynamic solution, but here's a pattern for finding the exact keywords you've got: Felice展示的是更具动态性的解决方案,但这里有一种模式可以找到您所获得的确切关键字:

"(?<=\b)((The)|(quick)|(brown)|(fox))(?=\b)"

Because of the leading and trailing capturing groups, it will only match whole words and not parts of them. 由于前导和尾随捕获组,它只匹配整个单词而不是它们的一部分。

Here's an example: 这是一个例子:

Regex foxey = new Regex(@"(?<=\b)((The)|(quick)|(brown)|(fox))(?=\b)");
foxey.Options = RegexOptions.IgnoreCase;
bool doesMatch = foxey.IsMatching("the Brown SexyFox Jumps soQuickly in the backyard...");

Edit - Regex engine: 编辑 - 正则表达式引擎:

Simply put, the Regex-engine walks through the input-string one character at a time, starting at the leftmost one, checking it against the first part of the regex-pattern we've written. 简单地说,正则表达式引擎一次遍历输入字符串中的一个字符,从最左边开始,检查它与我们编写的正则表达式模式的第一部分。 If it matches, the parser moves to the next character and checks it against the next part of the pattern. 如果匹配,则解析器移动到下一个字符并将其与模式的下一部分进行对比。 If it manages to successfully walk through the whole pattern, that is a match. 如果它设法成功遍历整个模式,那就是匹配。

You can read about how the internals of regex works just by searching for "regex engine" or something along those lines. 您可以通过搜索“正则表达式引擎”或其他内容来了解​​正则表达式的内部结构。 Here's a pick: http://www.regular-expressions.info/engine.html 这是一个选择: http//www.regular-expressions.info/engine.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM