E.g. match 353812345678 So far I have ^3538{1}[\d]{8} which开发者_如何学JAVA works but does not restrict the length. How do I make sure the length is only a maximum of 12 digits?
If you want the number to be the only thing in the string: ^3538\d{8}$
If you just want the number in a string: \b3538\d{8}\b
^
is the start-of-string anchor, while $
is the end-of-string anchor, so the first one restricts the number to be the only thing on the line.
In the other one, \b
means a word boundary, so it just means no other letters or digits may come directly before or after the number.
Also, note, in your original regex, the {1}
is redundant, and [\d]
means the same as \d
.
^3538{1}[\d]{8}[^\d]
will ensure you have 3538 followed by 8 digits and something that is NOT a digit -- thus limiting the length.
Add a dollar sign ($) at the end of the regex: ^3538{1}[\d]{8}$
精彩评论