Zachary Scott
Zachary Scott

Reputation: 21182

How to not match double spaces?

So far so good, but I would like to match "Health and Beauty"
instead of "Health and Beauty _ _ _" (trailing six spaces)

var a = Regex.Match("Health and Beauty      08/05/11 TO 08/11/11",
    @"^(?<dept>.*)" +
    @"(?<startdate>[0-9]{2}/[0-9]{2}/[0-9]{2})\s+TO\s+" +
    @"(?<enddate>[0-9]{2}/[0-9]{2}/[0-9]{2})$").Dump();

I tried a negative look ahead, but .* continued to match everything.

@"^(?<dept>.*(?!(\s\s)))\s+"  // should be "not followed by two spaces

I only have the option to match and extract, not to replace (the C# is for an example.)

Upvotes: 1

Views: 75

Answers (1)

Cameron
Cameron

Reputation: 98776

Change @"^(?<dept>.*)" to be non-greedy, and match on extra whitespace after:

@"^(?<dept>.*?)\s*"

Upvotes: 4

Related Questions