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.tsMatches
./src/index.ts
Input
../parent/file.jsMatches
../parent/file.js
Input
file.txtMatches
file.txt