.*[^\|]
means zero or more wild-cards (.*
) and one character that isn't a |
([^\|]
).
Also, you need to escape |
inside []
.
And Regex.Match
doesn't actually match, it searches, so you need ^
at the start of the regex (which indicates the start of string).
And the trailing .*
is thus also not required.
You instead want zero or more characters that aren't |
, like this:
"^[^|]*\|[^|]*\|[^|]*\|\|"
Test.
Why ".*\|.*\|.*\|\|.*"
didn't work:
Apart from the above reasons...
*
being greedy doesn't really change much (you can make it non-greedy / lazy by doing .*?
). The problem is that .
also matches |
and it backtracks, so .*
will include as many or as few |
's as required for it to match the string (yes, it will try to include more because it's greedy, but this doesn't change whether or not it finds something, only what it finds).
You can hack something together using lazy matching and possessive quantifiers, but it will end up being somewhat more complex and, more importantly, I suppose, C# doesn't support those.