1

I'm needing to locate any file that contains a single character filename with a 1-3 character extension.

I've spent some time searching for how to do this, but can't see to find the right combination of regex. I'm using a solution that requires boost regex, which I think is similar to perl regex.

Here are some examples should match:

1.exe
a.c
-.zip  (a hyphen is ok for the first part of the filename)

These examples should not match:

1 (doesn't have an extension)
1a.exe (has 2+ chars before the period)

Examples I've tried:

^[.]+\.[a-z0-9]{1,3}$
^[a-z0-9]+\.[a-z0-9]{1,3}$
^[\w]\.[\w]{1,3}$
^([0-9a-z]{1})\..{1,3}

Thank you for any help you can provide!

Vman
  • 11
  • 1

1 Answers1

0

You could do it like this:

^[\w\-]\.\w{1,3}$

Explanation

  • From the beginning of the string ^
  • Match a word character or a hyphen^[\w\-]
  • Match a dot \.
  • Match a word character between one and three times
  • The end of the string $

Or more restricted instead of the \w:

^[a-zA-Z\d\-]\.[a-zA-Z]{1,3}$

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • I think the more restricted one is actually the good one. `\w` matches any word character equal to `[a-zA-Z0-9_]` so that would allow an underscore as the filename and/or in the extension. – louisfischer Dec 30 '17 at 09:32