Regex for Unix File Path
Regex Pattern
^\/(?:[^\/\0]+\/)*[^\/\0]+$Unix absolute file path (must have at least one path component)
Quick Answer
The regex pattern for unix file path is `^\/(?:[^\/\0]+\/)*[^\/\0]+$`. Unix absolute file path (must have at least one path component). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| /home/user/file.txt | ✓ Matches |
| /var/log/syslog | ✓ Matches |
| /etc/config | ✓ Matches |
| relative/path | ✗ No match |
| C:\Windows | ✗ No match |
| / | ✗ No match |
Code Examples
javascript
const regex = /^\\/(?:[^\\/\0]+\\/)*[^\\/\0]+$/; const isValid = regex.test(value);
python
import re
pattern = r'^\/(?:[^\/\0]+\/)*[^\/\0]+$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^\/(?:[^\/\0]+\/)*[^\/\0]+$/ if value =~ pattern puts "valid" end
php
if (preg_match('/^\\/(?:[^\\/\0]+\\/)*[^\\/\0]+$/', $value)) {
echo "valid";
}java
String pattern = "^\\/(?:[^\\/\\0]+\\/)*[^\\/\\0]+$"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
File Extension
File extension at the end of a filename
Image File Extension
Common image file extensions
Video File Extension
Common video file extensions
Absolute Unix Path
Absolute filesystem path starting with /
Windows File Path
Windows absolute file path (C:\folder\file.txt)
Filename with Extension
Filename with at least one extension