This validation works well for allowing alphanumeric chars, spaces, and a dash, but I have not been able to set the max length to 23.
Regex: (^\w+\s*(-?)(\s*\w+\s*)(\w+)$){0,23}
The cases I need to pass:
Cases I need to fail:
By combining the interval quantifier with the surrounding start- and end-of-string anchors, the regex will fail to match if the subject text's length falls outside the desired range.
To show a range of characters, use square backets and separate the starting character from the ending character with a hyphen. For example, [0-9] matches any digit. Several ranges can be put inside square brackets. For example, [A-CX-Z] matches 'A' or 'B' or 'C' or 'X' or 'Y' or 'Z'.
The indexing is done within the maximum range. It means that we cannot store the 2147483648th character. Therefore, the maximum length of String in Java is 0 to 2147483647. So, we can have a String with the length of 2,147,483,647 characters, theoretically.
It may be more convenient to check the length separately, but you can use a lookahead to confirm that the entire expression is between 0 and 23 characters.
(?=^.{0,23}$)(^\w+\s*(-?)(\s*\w+\s*)(\w+)$)
http://rubular.com/r/GVIbG8hDKz
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With