regex - Test filename with regular expression -


i trying test filename string pattern:

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

i want ensure there 3 letter extension , allow letters, numbers , these symbols: - _ , \s precede don't want have include of letters , characters in filename. use * instead of + match 0 or more wouldn't valid filename.

here examples of how 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 great if point me in right direction.

thanks

you use these expressions instead:

  • \w - same [a-za-z0-9_]
  • \d - same [0-9]
  • \. - same [.]{1}

which make regex:

^[\w,\s-]+\.[a-za-z]{3}$ 

note literal dash in character class must first or last or escaped (i put last), put in middle, incorrectly becomes range.

notice last [a-za-z] can not replaced \w because \w includes underscore character , digits.

edited: @tomasz right! \w == [a-za-z0-9_] (confirmed here), altered answer remove unnecessary \d first character class.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -