DD
DevDash

Regex for Absolute Unix Path

Regex Pattern

^\/(?:[^\/\0]+\/)*[^\/\0]*$

Absolute filesystem path starting with /

Quick Answer

The regex pattern for absolute unix path is `^\/(?:[^\/\0]+\/)*[^\/\0]*$`. Absolute filesystem path starting with /. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
/home/user/file.txt✓ Matches
/var/log/syslog✓ Matches
/✓ Matches
relative/path✗ No match
C:\\Windows✗ No match
../parent✗ 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

Want API access + no ads? Pro coming soon.