java - how to limit the number of "/" in a string -
how use lookahead assertion limit range number of "/"
i have tired following
^(?=/{1,3})$
but doesn't work
the easiest solution use negative lookahead:
^(?!(?:[^/]*/){4}) that means string cannot contain 4 slashes.
this assumes allow other characters between slashes, maximum of 3 slashes.
a positive version ^(?=[^/]*(?:/[^/]*){0,3}$) or ^[^/]*(?:/[^/]*){0,3}$, without lookahead. of course, problem trivial without regular expressions, if possible.
lets try break last 1 down:
^- start of string.[^/]*- characters not slashes (or none)(?: )- logical group. similar(), not capture result (we not need after validation)/[^/]*- slash, followed non-slash characters.{0,3}- 0 3 times.$- end of string.
Comments
Post a Comment