You are almost there, all you need is a binding ( ^ ) and the end of the binding ( $ ):
^[0-9]{1,45}$
\d is short for character class [0-9] . You can use this as:
^\d{1,45}$
Anchors force the pattern to match entire inputs, not just parts of it.
Your regular expression [0-9]{1,45} looks for 1 to 45 digits, so a line like foo1 will also match, since it contains 1 .
^[0-9]{1,45} looks for 1 to 45 digits, but these digits should be at the beginning . It matches 123 , but also 123foo
[0-9]{1,45}$ searches from 1 to 45 digits, but these digits must be at the end of the end . It matches 123 , but also foo123
^[0-9]{1,45}$ looking for 1 to 45 digits, but these digits should be both at the beginning and at the end of the input, so it must be an integer .
codaddict Oct 19 '10 at 12:00 2010-10-19 12:00
source share