Regex to validate a character at start and end but also multiple occurences in between -
i need prevent character / occurring in java string @ start , end , // anywhere alphanumerics allowed along .,()?'+/
i have ^[^/]?([\w\-\?:().,'+]/?)*[^/]$
the main problem allows invalid characters # , ## @ start.
also tried ^[^/]?([^\w_\-\?:().,'+]/?)*[^/]$
remove _ underscore issue caused \w
so these invalid
/ a//a a/ # ##
but these valid
a/a a/a.,()+a
give chance simple. there's no need negated character classes ([^x]
- "anything x") or negative lookarounds ((?<!x)
, (?!x)
- "nothing, or not x"). know characters allowed everywhere in string, spell out:
^[a-za-z0-9?:().,'+-]+(?:/[a-za-z0-9?:().,'+-]+)*$
you don't need specify slash (/
) can't first, list characters can. , when slash appear, make sure it's followed @ least 1 of other allowed characters. , anchors (^
, $
) ensure nothing else gets in.
Comments
Post a Comment