48

I am trying to test a filename string with this pattern:

^[A-Za-z0-9-_,\s]+[.]{1}[A-Za-z]{3}$

I want to ensure there is a three letter extension and allow letters, numbers and these symbols: - _ , \s to precede it but I don't want to have to include all of the letters and characters in the filename. I could just use a * instead of a + but that would match 0 or more which wouldn't be a valid filename.

Here are some examples of how the rule should react:

Correct file name.pdf - true
Correct, file name.pdf - true
Correct_file_name.pdf - true
Correctfilename.pdf - true
Incorrect &% file name.pdf - false
Incorrect file name- false

It would be great if someone could point me in the right direction.

Thanks

eb_Dev
  • 873
  • 2
  • 13
  • 25

2 Answers2

72

You could use these expressions instead:

  • \w - is the same as [a-zA-Z0-9_]
  • \d - is the same as [0-9]
  • \. - is the same as [.]{1}

Which would make your regex:

^[\w,\s-]+\.[A-Za-z]{3}$

Note that a literal dash in a character class must be first or last or escaped (I put it last), but you put it in the middle, which incorrectly becomes a range.

Notice that the last [a-zA-Z] can not be replaced by \w because \w includes the underscore character and digits.

EDITED: @tomasz is right! \w == [a-zA-Z0-9_] (confirmed here), so I altered my answer to remove the unnecessary \d from the first character class.

LipESprY
  • 291
  • 2
  • 8
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 5
    `\w` is usually [a-zA-Z0-9_]; that's why I'm not a big fan of the class as you have to remember this specific word definition. – tomasz Jul 20 '11 at 21:44
  • Fantastic, that works a treat thanks! Can you explain why my pattern didn't work? Is it the fact i missed out the \ when defining my full stop? – eb_Dev Jul 20 '11 at 22:14
  • Sorry being lazy, just tested it with the \ and my rule worked. Thanks again. – eb_Dev Jul 20 '11 at 22:16
  • 1
    For more generic usage, could be ^[\w,\s-]+\.[A-Za-z]+$ which will match all extensions which have at least 1 char. This would now accept a file such as web.config – Talon Jun 26 '15 at 13:25
  • 4
    I don't this will work if the file is named something.mp4 – Eran Chetzroni Mar 20 '16 at 08:11
  • Why accepting extensions with exactly 3 characters and and no numbers? – yannicuLar Apr 04 '17 at 16:38
  • 1
    @yannicuLar because the question asks to match extensions with 3 *letters*, not *characters*. It's trivial to adapt the extension part to your needs, eg `[a-zA-Z0-9]{2,4}` – Bohemian Apr 04 '17 at 17:34
  • Ran into that too, so I posted my own answer for DDG-ers. – MS Berends Nov 23 '17 at 12:01
22

This question was asked specifically to allow a three letter extension.

For anyone coming from DuckDuckGo like me (yes, you shouldn't use Google :p), this regex tests for valid filenames and file paths on Windows, Unix and macOS:

^[^<>:;,?"*|/]+$

or even

^[^~)('!*<>:;,?"*|/]+$

Note: On Windows, \is not allowed in filenames, but the above regex works for checking valid paths on Windows. Include \\ between the brackets ^[...]+$ if you want it to check for valid filenames and not checking paths.

mruanova
  • 6,351
  • 6
  • 37
  • 55
MS Berends
  • 4,489
  • 1
  • 40
  • 53