DD
DevDash

Regex for Filename without Extension

Regex Pattern

^[a-zA-Z0-9][a-zA-Z0-9_-]*$

Filename with no dots or extension

Quick Answer

The regex pattern for filename without extension is `^[a-zA-Z0-9][a-zA-Z0-9_-]*$`. Filename with no dots or extension. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
Makefile✓ Matches
README✓ Matches
Dockerfile✓ Matches
LICENSE✓ Matches
file.txt✗ No match
.hidden✗ No match
has space✗ No match

Code Examples

javascript

const regex = /^[a-zA-Z0-9][a-zA-Z0-9_-]*$/;
const isValid = regex.test(value);

python

import re
pattern = r'^[a-zA-Z0-9][a-zA-Z0-9_-]*$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^[a-zA-Z0-9][a-zA-Z0-9_-]*$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^[a-zA-Z0-9][a-zA-Z0-9_-]*$/', $value)) {
    echo "valid";
}

java

String pattern = "^[a-zA-Z0-9][a-zA-Z0-9_-]*$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.