Regular Expressions 101

Community Patterns

Get the root of a Windows path

0

Regular Expression
PCRE (PHP <7.3)

/
^([a-zA-Z]:(?=\\)|\\\\(?:[^\*:<>?\\\/|]+\\[^\*:<>?\\\/|]+|\?\\(?:[a-zA-Z]:(?=\\)|(?:UNC\\)?[^\*:<>?\\\/|]+\\[^\*:<>?\\\/|]+)))
/

Description

This extracts the "root" (e.g. drive) from a Windows path. See the unit tests for examples

Submitted by J5lx - 9 years ago