Search code examples
c#regexwildcardmaxlength

Regex - only matching wildcard of a certain length or less


Is there a way to have a Regex statement search for a wildcard with a maximum length? For instance:

somestuff.*morestuff

If I wanted the above to match

somestuffblahmorestuff

but not

somestuffblahblahmorestuff

Is this possible?


Solution

  • To match a known length use .{2,5} where the 2 is the minimum number of characters and 5 is the max. both values are optional but you do need one or the other

    More can be read on this topic here