Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex must start with a letter or number, string can only have letters, numbers or slashes, and no double slashes

Tags:

regex

I am trying to check some strings. Here are the parameters.

  1. A string must start with a letter or number
  2. A string can only contain letters, numbers or slashes
  3. A string cannot have a double slash (ex: "api//go")

Good Strings:

go
go2/api/hello
go/api45

Bad Strings:

/go        (can't begin with a slash)
go//api    (can't have a double slash)
go/api%    (can't contain non number, letter or slash)

I've been trying to use RegExr.com but to no avail. I have been trying with this expression:

^[^\/](([0-9A-Za-z])+(\/)?)+

but it does not quite work.

like image 349
Jake.JS Avatar asked Feb 24 '15 23:02

Jake.JS


1 Answers

You might try something like this (I hope whatever flavor you're using has lookahead!):

^(?!.*\/\/)[A-Za-z0-9][A-Za-z0-9\/]*$

Please see Regex 101 Demo for complete description and test strings.

like image 90
David Faber Avatar answered Sep 21 '22 13:09

David Faber