简体   繁体   中英

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...

If there is any match with these words (not Casesensitive):

The, the, brown, Brown, fox, Fox, quick, Quick

Then I can say that return value is 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.

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.
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
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:

"(?<=\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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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