user2609388
user2609388

Reputation: 5

Controlling number of characters in expression?

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:

Upvotes: 0

Views: 64

Answers (2)

Bohemian
Bohemian

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

poncha
poncha

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

Related Questions