BourneShady
BourneShady

Reputation: 935

regex for windows filenames laravel 4.2

I have this controller that updates the filename of a file. At first, I did not know about the certain validations when editing a filename. So I had an error when renaming it. So now what I do is to implement a regex in my validations.

Here's how it looks like:

$rules = array(
        'eFile' => 'required|min:2|max:200| regex: /^(?!\.)(?!com[0-9]$)(?!con$)(?!lpt[0-9]$)(?!nul$)(?!prn$)[^\|\*\?\\:<>/$"]*[^\.\|\*\?\\:<>/$"]+$/ | unique:nsa_fileuploads,filename,' . $id . ',fileid'
    );

The regex pattern:

regex: /^(?!\.)(?!com[0-9]$)(?!con$)(?!lpt[0-9]$)(?!nul$)(?!prn$)[^\|\*\?\\:<>/$"]*[^\.\|\*\?\\:<>/$"]+$/

but once executed, I get the following error:

preg_match(): No ending delimiter '/' found

Any ideas on what I'm doing wrong?

Upvotes: 0

Views: 68

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627103

You can use an array here as your pattern contains a pipe symbol. Even if it is not an alternation operator and is inside a character class, the pipe requires using an array.

Use

'eFile' => [
  'required', 'min:2', 'max:200', 
  'regex:/^(?!\.)(?!com[0-9]$)(?!con$)(?!lpt[0-9]$)(?!nul$)(?!prn$)[^|*?\\:<>\/$"]*‌​[^.|*?‌​\\:<>\/$"]+$/', 
  'unique:nsa_fileuploads,filename,' . $id . ',fileid' 
]

As for the regex itself, note that you do not have to escape special characters other than \ (and also - (that is not at the start/end of the char class) and ^ (if not at the start of the character class), and ] (if not at the start of the character class)) and / (because it is a regex delimiter) inside the regex pattern.

Upvotes: 1

Related Questions