Web
File Path with Extension Regex
This pattern matches file paths that end with a file name and an extension, such as C:\Users\test\report.pdf, /var/log/app.log or docs/readme.md. It accepts an optional Windows drive letter or leading slash, folders separated by forward or back slashes, and names containing letters, digits, spaces, dots, dashes and underscores. It is handy for upload forms, build tool configuration, log parsing and checking that a value points at a file rather than a folder. The honest limit is that it ignores operating system rules such as reserved names and many valid Unicode file names.
Pattern (JavaScript)
/^(?:[A-Za-z]:[\\/]|/)?(?:[\w .-]+[\\/])*[\w .-]+\.[A-Za-z0-9]{1,8}$/Starts at the start of the line/string, then (a letter, then the character ":", then one of the characters \\/ OR the character "/"), optionally (zero or one time), then (one of the characters \w .-, one or more times, then one of the characters \\/), zero or more times, then one of the characters \w .-, one or more times, then the character ".", then a letter or digit, between 1 and 8 times, then the end of the line/string.
Examples
Matches
- ✓ C:\Users\test\report.pdf
- ✓ /var/log/app.log
- ✓ docs/readme.md
Does not match
- ✗ /var/log/
- ✗ README
- ✗ C:\Users\test\
Try it
0 matches (JavaScript engine)
C:\Users\test\report.pdf
/var/log/app.log
docs/readme.md
/var/log/
README
C:\Users\test\Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:[A-Za-z]:[\\\/]|\/)?(?:[\w .-]+[\\\/])*[\w .-]+\.[A-Za-z0-9]{1,8}$/g;
const text = "C:\\Users\\test\\report.pdf";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:[A-Za-z]:[\\/]|/)?(?:[\w .-]+[\\/])*[\w .-]+\.[A-Za-z0-9]{1,8}$')
text = 'C:\\Users\\test\\report.pdf'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '#^(?:[A-Za-z]:[\\\/]|/)?(?:[\w .-]+[\\\/])*[\w .-]+\.[A-Za-z0-9]{1,8}$#';
$text = 'C:\\Users\\test\\report.pdf';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:[A-Za-z]:[\\\\/]|/)?(?:[\\w .-]+[\\\\/])*[\\w .-]+\\.[A-Za-z0-9]{1,8}$");
String text = "C:\\Users\\test\\report.pdf";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:[A-Za-z]:[\\/]|/)?(?:[\w .-]+[\\/])*[\w .-]+\.[A-Za-z0-9]{1,8}$`)
text := "C:\\Users\\test\\report.pdf"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:[A-Za-z]:[\\/]|/)?(?:[\w .-]+[\\/])*[\w .-]+\.[A-Za-z0-9]{1,8}$");
var text = "C:\\Users\\test\\report.pdf";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(?:[A-Za-z]:[\\/]|/)?(?:[\w .-]+[\\/])*[\w .-]+\.[A-Za-z0-9]{1,8}$").unwrap();
let text = "C:\\Users\\test\\report.pdf";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:[A-Za-z]:[\\\/]|\/)?(?:[\w .-]+[\\\/])*[\w .-]+\.[A-Za-z0-9]{1,8}$/
text = 'C:\\Users\\test\\report.pdf'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I capture just the extension?
- Wrap the final [A-Za-z0-9]{1,8} in a capturing group and read it after a match. Lowercase it before comparing, since extensions like PDF and pdf are the same type.
- Does it accept UNC network paths?
- No, paths starting with \\server\share are not covered. Add an alternative for the double backslash prefix if your users work with network shares.
- Can I restrict it to certain file types?
- Replace the extension part with an alternation such as (?:pdf|docx|png). Add the i flag if you want to accept uppercase extensions too.