1

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?

Wilson
  • 8,570
  • 20
  • 66
  • 101

3 Answers3

4

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

Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43
2

In regex:

{n}
Matches the previous element exactly n times.

{n,}
Matches the previous element at least n times.

{n,m}
Matches the previous element at least n times, but no more than m times.

For example:

,\d{3} matches ,876, ,543, and ,210 in 9,876,543,210

\d{2,} matches 166, 29, 1930

\d{3,5} matches 19302 in 193024

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ria
  • 10,237
  • 3
  • 33
  • 60
  • 1
    This answer has been added to the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), under "Quantifiers" – aliteralmind Apr 10 '14 at 00:13
0
somestuff.{4,7}morestuff

{min, max} is the syntax to specify the number of repetition.

n1xx1
  • 1,971
  • 20
  • 26