C# Regex check if path starts with specific path but does not contain a slash after

I’m new to Regex so please take it easy!

I need to check in C# using Regex if a folder name start with a base folder name and doesn’t have any slash after the base, ie:

Base Folder: user/folder/

Check: user/folder/text.txt (IS VALID)
Check: user/folder/subfolder/test2.txt (Not valid since there is a slash after)
Check: user/folder/ (VALID)
Check: user/sub1/ (INVALID not same base)

Thanks,

Tried: ^(user\/local\/)[^\/]

But couldn’t make it work.

>Solution :

^              # At the beginning of the string,
user/folder/   # match 'user/folder/' literally,
[^/]*          # then 0 or more non-forward-slash character
$              # right before the end of string.

/ doesn’t mean anything special so there’s no need to escape it.

Try it on regex101.com.

Try it on dotnetfiddle.net.

Leave a Reply