Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Initialization problems

I'm working with a bit of legacy code, and I'm trying to add a new regex to a validator's enumerator, for our purposes labeled as "URL". As I understand it, each part of the enumerator can be called into a separate regex. At any rate, here's what I've got (and others follow, hence the trailing comma):

URL("[a-zA-Z0-9\r#;?-\'.:,!/\\s]{1,250}", "Up to 250 letters (upper and lower case), numbers, #, ;, ?, -, ', ., :, comma, !, /, blankspace and carriage return"),

I made a simple JUnit test to see if it was working correctly. It is not.

Caused by: java.util.regex.PatternSyntaxException: Illegal character range near index 15
[a-zA-Z0-9
#;?-'.:,!/\s]{1,250}
               ^

I'm trying to restrict input of type URL to be between 1 and 250 characters, which is what I thought I was doing, but Eclipse seems to take offense to this comma (I assume the comma is index 15). What am I doing wrong?

like image 873
Raven Dreamer Avatar asked Feb 25 '23 21:02

Raven Dreamer


1 Answers

The problem is probably here:

?-'

This is being interpreted as a range from the character ? to the character ', which is invalid because ? has code point 63, while ' has code point 39. Ranges should go from low to high. But you probably didn't intend to have a range here. I imagine you intended a literal hyphen.

To fix the error, try escaping the hyphen:

?\\-'

The reason why the error message indicator in the wrong place is because of the \r in the regular expression. If you replace the newline with another character the indicator lines up with the incorrect range:

[a-zA-Z0-9_#;?-'.:,!/\s]{1,250}
               ^

You could also use \\r instead of \r so that the error message is printed correctly.

like image 54
Mark Byers Avatar answered Feb 27 '23 11:02

Mark Byers