Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression matching /admin/something not administrator

Tags:

regex

php

Hi I want to match strings like /admin/ or /admin/something or /admin but not /administration or /administrator. I wrote this regular expression:

$pattern = '%^/admin(/|/[a-zA-Z0-9_-]{1,})?$%'

and it works, but it is something ugly. I want to know can it be written more cleaner and understandable?


1 Answers

This regex should work for you:

$pattern = '%^/admin(/[\w-]*)?$%'

This will match any of:

  • /admin
  • /admin/
  • /admin/foobar
like image 57
anubhava Avatar answered Sep 03 '25 19:09

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!