File Extension Extraction Regex

Extract file extensions from filenames. Matches extensions like .pdf, .jpg, .txt at the end of filenames.

Pattern & Test String

About This Pattern

Pattern: \.[a-zA-Z0-9]+$

Flags: g

Results

Enter a regex pattern to see the results here.

Frequently Asked Questions

Does this handle multiple dots in filenames?

Yes, this pattern matches only the last extension. For 'archive.tar.gz', it matches '.gz'. To match '.tar.gz', use a more specific pattern.

What about files without extensions?

This pattern won't match files without extensions. Check if the pattern matches before assuming an extension exists.

How do I validate specific file types?

Modify the pattern: \.(pdf|jpg|png|doc)$ to match only specific extensions.

Practical Examples

File Upload Validation

Extract and validate file extensions during file uploads.

File Organization

Group or sort files by extension type.

MIME Type Detection

Extract extensions to determine MIME types for file handling.

Common Issues & Solutions

To extract without the dot, use a capture group: \.([a-zA-Z0-9]+)$ and access the captured group.

For case-insensitive matching (treating .PDF and .pdf the same), enable the 'i' flag.

This pattern doesn't validate if the extension is valid or recognized. Check against a whitelist of allowed extensions.

Hidden files starting with dot (.gitignore) may cause issues. Add logic to check for this case.