Validate the path of an url

I'm trying to validate an url without domain, just the path and params.

The regular expression that I'm using do most of the work, but It has some errors that I dont know how to prevent (I'm pretty noob with regexp):

/^(\/([\w#!:.?+=&%@!\-\/])+)$/i

The next example are correctly validated

/asd.jsp -> true
/asd/asd.jsp -> true
/asd/asd.jsp?bar=baz&inga=42&quux -> true
/asd/asd.jsp?bar=ba z&inga=42&quux -> false

But this ones arent correct ulrs and them gives me true too:

/asd/asd./jsp -> true :(
/asd/asd.jsp/ -> true :(
/asd./asd.jsp -> true :(
/asd///asd.jsp -> true :(
/asd/asd.jsp&bar=baz?inga=42?quux -> true :(

Do you recommend to use a function instead of a regex?

Very much thanks!

Answers


Try this:

^(\\/\\w+)+\\.\\w+(\\?(\\w+=[\\w\\d]+(&\\w+=[\\w\\d]+)+)+)*$

I already escaped special characters, so you can directly use it in java. By the way, /asd/asd.jsp?bar=baz&inga=42&quux is not a valid URL.

Unescaped Regex:

^(\/\w+)+\.\w+(\?(\w+=[\w\d]+(&\w+=[\w\d]+)+)+)*$

^(\/\w+)+(\.)?\w+(\?(\w+=[\w\d]+(&\w+=[\w\d]+)*)+){0,1}$

VALID:

/test
/test/test
/test.jsp
/test.jsp?v=1
/test.jsp?v=1&v=2
/test/test.jsp?v=1&v=2

INVALID:

/test/test./jsp
/test/test.jsp/
/test./test.jsp
/test///test.jsp

Need Your Help

Create an array of integers property in Objective C

objective-c arrays properties

I'm having troubles creating a property of an array of integers in Objective C.

When requesting com port returns the same request

java serial-port at-command jssc

I`m trying to send AT-command via COM-port, but reseived only the same command.