3

I have some urls and I would like to make sure they have at least 4 forward slashes as some of my URLS have less. For example:

Pass: http://localhost:2000/machine/my-test-machine/3

Fail: http://localhost:2000/my-test-machine

Any help would be much appreciated. Thanks Dave

dave
  • 43
  • 6

3 Answers3

1

You can try following regex,

\/\/(.*\/){3}

Working Demo

apgp88
  • 955
  • 7
  • 14
1

Just match it 4 times:

(?:.*?/){4}

See live demo.

The reluctant quantifier *? will ensure that slashes are not skipped over when matching (eliminating back tracking)

Your regex engine (unspecified) may require the forward slash to be escaped, ie \/

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

Use a lookahead such as:

^(?=(?:[^\/]*\/){4,})(.*)

Demo

dawg
  • 98,345
  • 23
  • 131
  • 206