A regular expression must begin with a letter or number, a line can only contain letters, numbers or slashes, and double slashes

I am trying to check some lines. Here are the options.

  • The line must begin with a letter or number
  • A string can only contain letters, numbers, or slashes
  • A string cannot have a double slash (for example: api // go)

Good lines:

go go2/api/hello go/api45 

Bad lines:

 /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 am trying to use RegExr.com, but to no avail. I tried with this expression:

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

but it does not work.

+5
source share
2 answers

You can try something like this (I hope that any taste you use has a look!):

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

Please view the Regex 101 Demo for a full description and test lines.

+2
source

Here you go:

 ^[0-9a-z](\/?[0-9a-z])*\/?$ 

Requires /i (case insensitive modifier) ​​in the current form. It is quite simple and does not need views.

Explanation:

 ^ Starts with [0-9a-z] 1 Alphanumeric character ( Start a repeatable group \/? Optional / [0-9a-z] 1 Alphanumeric character )* Repeat the group zero or more times \/? Allow for an ending slash $ String must end 
+1
source

Source: https://habr.com/ru/post/1214123/


All Articles