Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help with a file path validation regular expression

Tags:

regex

I am using a RegularExpressionValidator in visual studio and I am struggling to create the correct regular expression for my needs. Here is what I want:

The input can contain any character except <>:"/|?* Also, the input can not contain two backslashes in a row

So, your\mom would be ok but your\\mom would fail as would your*mom

The closest I have come at this point is something like

^(?=.*[^<>:"/|?*])(?:[^\\]+|\\(?:$|[^\\])).{0,100}$

but it doesn't work.

like image 669
user761605 Avatar asked Dec 15 '25 14:12

user761605


1 Answers

^(?!.*\\\\)[^<>:"/|?*]*$

should do it.

(?!.*\\\\) asserts that there are no two backslashes in a row in the string.

[^<>:"/|?*]* matches any number of characters except the ones inside the character class.

That is, unless you're talking about the regex features of Visual Studio (the IDE environment itself) which has a wildly nonstandard regex flavor.

like image 147
Tim Pietzcker Avatar answered Dec 19 '25 05:12

Tim Pietzcker



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!