Michal Czardybon
Michal Czardybon

Reputation: 2865

Regex (.NET) for strings ending with FIRST OCCURENCE of "xyz"

I need a regex that matches first "xyz" and all characters that come before. For example, for "abxyzcdxyz" it should match "abxyz". I was trying with pattern ".*xyz", but it matches the entire string.

Upvotes: 1

Views: 244

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545618

Try non-greedy matching:

.*?xyz

*? is a non-greedy quantifier, i.e. it matches zero or more occurrences, but as few as possible.

Upvotes: 5

Related Questions