Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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/

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading