Using regular expressions to validate a numeric range

Using regular expressions to validate a numeric range To be clear: When a simple if statement will suffice if(num < -2055 || num > 2055) { throw new IllegalArgumentException(“num (” + num + “) must be between -2055 and 2055”); } using regular expressions for validating numeric ranges is not recommended. In addition, since regular … Read more

regex word boundary excluding the hyphen

You can use a lookahead for this, the shortest would be to use a negative lookahead: type ([a-z])(?![\w-]) (?![\w-]) would mean “fail the match if the next character is in \w or is a -“. Here is an option that uses a normal lookahead: type ([a-z])(?=[^\w-]|$) You can read (?=[^\w-]|$) as “only match if the … Read more