Reputation: 5
Can I somehow limit the number of characters of this whole expression?
I want to limit number of characters in an expression like:
([0-9]+(\.?[0-9]+)*)
I want the strings following the limit to be accepted. For example, if the limit is 4:
12.3
gets accepted012
doesn't12345
doesn't.Upvotes: 0
Views: 64
Reputation: 425418
Use an anchored look ahead (for example max 33):
^(?=.{0,33}$)([0-9]+(\.?[0-9]+)*)
To make it exactly 33, remove the 0,
To ignore the number of periods in the limit
^(?=(\.*[^.]){0,33}\.*$)([0-9]+(\.?[0-9]+)*)
Upvotes: 1
Reputation: 7866
Regex quantifiers:
{n}
- Exactly n
appearances{n,m}
- Minimum n
occurences, maximum m
occurences{,m}
- Minimum zero, maximum m
{n,}
- Minimum n
, maximum infinity.*
- Any amount. Equivalent to {0,}
+
- One or more. Equivalent to {1,}
?
- zero or one. Exuivalent to {0,1}
This about sums it up.
Upvotes: 1