Poma
Poma

Reputation: 8484

How to match end of string in lookahead?

Why this do not match and how to make it work?

Regex.Match("qwe", ".*?(?=([ $]))");

I should match everything to first space or to the end of line.

Upvotes: 2

Views: 1564

Answers (2)

Mark Byers
Mark Byers

Reputation: 839114

Your specific problem is that you need to use an alternation, not a character class, because inside a character class the $ symbol literally means "match a dollar symbol", and does not have its special meaning end-of-line in that context.

( |$)

It seems however that your example is a bit strange. It would be simpler to match any character except space, then you wouldn't need a lookahead at all.

Upvotes: 4

hsz
hsz

Reputation: 152294

Try with:

Regex.Match("qwe", "^([^ ]*)");

Upvotes: 2

Related Questions