Python (re)

FTP URL in PY

Match FTP and FTPS URLs, capturing optional credentials, host, port, and path.

Try it in the PY tester →

Pattern

regexPY
ftps?:\/\/(?:([^:@\s]+)(?::([^@\s]*))?@)?([^:\/\s]+)(?::(\d+))?(?:\/\S*)?   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"ftps?:\/\/(?:([^:@\s]+)(?::([^@\s]*))?@)?([^:\/\s]+)(?::(\d+))?(?:\/\S*)?")
input_text = "Source: ftp://anon:guest@files.example.com/pub/data.csv"
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

ftps?:\/\/ matches both `ftp://` and `ftps://`. The optional auth group captures user and password. The host group captures the server. (?::(\d+))? optionally captures the port (default is 21). (?:\/\S*)? optionally captures the path.

Examples

Input

Source: ftp://anon:guest@files.example.com/pub/data.csv

Matches

  • ftp://anon:guest@files.example.com/pub/data.csv

Input

ftps://secure.host:990/private

Matches

  • ftps://secure.host:990/private

Input

no ftp link

No match

Same pattern, other engines

← Back to FTP URL overview (all engines)