Python (re)

Unix File Path in PY

Matches absolute Unix/Linux file paths.

Try it in the PY tester →

Pattern

regexPY
^(/[^/\0]+)+/?$

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+.

How the pattern 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/documents

Matches

  • /home/user/documents

Input

/var/log/

Matches

  • /var/log/

Input

relative/path

No match

Same pattern, other engines

← Back to Unix File Path overview (all engines)