Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I recognize strings that do not end with a slash character ('/') using a regex?

Tags:

regex

How can i match a string that does not finish with / . I know I can do that /\/$/ and it will match if string does finish with /, but how can I test to see if it doesn't?

like image 870
Nikolay Avatar asked Sep 02 '25 11:09

Nikolay


2 Answers

You can use a negative character class:

/[^\/]$/

This however requires that the string contains at least one character. If you also want to allow the empty string you can use an alternation:

/[^\/]$|^$/

A different approach is to use a negative lookbehind but note that many popular regular expression engines do not support lookbehinds:

/(?<!\/)$/
like image 191
Mark Byers Avatar answered Sep 04 '25 02:09

Mark Byers


You can say "not character" by doing [^...]. In this case, you can say "not backslash by doing": /[^\/]$/

like image 30
Donald Miner Avatar answered Sep 04 '25 01:09

Donald Miner