ybakos
ybakos

Reputation: 8630

What regex can isolate the directory hierarchy from a path?

Say you have a path that can be one of:

  1. Foo/Bar/Baz.txt (a directory hierarchy followed by a file containing a single dot)
  2. Foo/Bar/ (a directory hierarchy ending in a trailing slash)
  3. Foo/Bar (a directory hierarchy that does not end with a trailing slash)

What's an effective regex to capture just the directory hierarchy from paths like these, resulting in capturing just Foo/Bar ?

Upvotes: 1

Views: 179

Answers (1)

hjpotter92
hjpotter92

Reputation: 80629

EDIT:

^(.*?)(?:/(?:[^/.]*\.[^/.]+)?)?$

Regular expression visualization

Upvotes: 2

Related Questions