Python (re)

Relative Path in PY

Matches relative file paths (./, ../, or path/to/file).

Try it in the PY tester →

Pattern

regexPY
^(\.{1,2}\/)?([^\/\0]+\/)*[^\/\0]+$

Python (re) code

pyPython
import re

pattern = re.compile(r"^(\.{1,2}\/)?([^\/\0]+\/)*[^\/\0]+$")
input_text = "./src/index.ts"
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

`^(\.{1,2}\/)?` optionally matches `./` or `../` prefix. `([^\/\0]+\/)*` matches zero or more directory segments. `[^\/\0]+$` matches the final file segment.

Examples

Input

./src/index.ts

Matches

  • ./src/index.ts

Input

../parent/file.js

Matches

  • ../parent/file.js

Input

file.txt

Matches

  • file.txt

Same pattern, other engines

← Back to Relative Path overview (all engines)