File & Path
Unix File Path
Matches absolute Unix/Linux file paths.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^(/[^/\\0]+)+/?$", "");
const input = "/home/user/documents";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
pyPython
import re
pattern = re.compile(r"^(/[^/\0]+)+/?$")
input_text = "/home/user/documents"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(/[^/\0]+)+/?$`)
input := `/home/user/documents`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
regexengine-agnostic
^(/[^/\0]+)+/?$Raw source: ^(/[^/\0]+)+/?$
How it works
`^(/[^/\0]+)+` matches one or more path segments beginning with `/`, excluding null and additional slashes within segments. `/?$` allows optional trailing slash.
Examples
Input
/home/user/documentsMatches
/home/user/documents
Input
/var/log/Matches
/var/log/
Input
relative/pathNo match
—Common use cases
- •Path validation
- •File system tools
- •Shell scripting
Related patterns
Windows File Path
File & PathMatches absolute Windows file paths (e.g., C:\Users\file.txt).
Relative Path
File & PathMatches relative file paths (./, ../, or path/to/file).
File Extension
File & PathCaptures the file extension from a filename.
Hidden File (Dotfile)
File & PathMatches Unix-style hidden files (dotfiles) like .gitignore or .env.