0

Linux has a maximum filename length of 255 characters for most filesystems, and a maximum path of 4096 characters. If the foldername is longer than 255 it cannot be created.

/^[a-z0-9\s_@-/.]+$/i is a good regex (I need special characters in the path also), but I need to modify it, so that it limits the string length to 4095 and the folder name length to 255.

So limiting it is no issue /^[a-z0-9\s_@-/.]{1-4095}$/i ,but that still doesn't solve the maximum folder name size issue.

Sample that should validate:
/whatever/thisisnotapornstash/StillNot255CharactersButTheNextFolderIs/BPLrmwQRjm‌​twIGEMDcgGk1BCRY6ZkKzsHoWqJNzGxCzlGTSZkfOei0QD2S3bGfqSMJMPxuvgHhUJotNgh3hGDYD01n5‌​6JiZy32JygaHHDLQbGWtkbFJy5BrMP5s6eL6V8Kcft71CxHZUMEEJ2LbYExYtPxaWuQ9USUCxbt7wTIjA‌​LoLN6aHW0GovD5euXWsYuOsqvyGuzJqjaohM9FFNmMz7ul0R4HxzTWWQqCZ8hp6O2yipRTs5k4RmGCTLf‌​nY/

What I have come up with until now: data-ng-maxlength="4095" (This solves the filepath maximum length) data-ng-pattern=/^[a-z0-9\s_@/.-] (This is where I should limit the maximum character number to 255 between two slashes.)

Here is where I am testing it: https://regex101.com/r/kV7dL2/3

Dezso Gabos
  • 2,297
  • 5
  • 21
  • 29

3 Answers3

2
/^\/?(([0-9a-z]{0,255})||([0-9a-z]{1,255})?([0-9a-z]{1,255}\/)+)[0-9a-z]{1,255}\/?$/i

This regex will test if all the folder names in the path are between 1 and 255 characters long. The forward slashes from the beginning/ending of the path are optional.

Dan Radu
  • 36
  • 3
1

How about:

/^(?=(?:\/[a-z0-9s_@.-]{1,255})+).{1,4095}$/i

The lookahead limits the length of directories to 255 characters then we test that the total length is limited to 4095 char.

You may use more generic regex like:

/^(?=(?:\/[^/]{1,255})+).{1,4095}$/i
Toto
  • 89,455
  • 62
  • 89
  • 125
  • I tried this regex, and it accepts any folder length. It should reject this: /home/gd/testwithlongdirectory/BPLrmwQRjsadsadassadsadasmtwIGEMDcgGk1BCRY6ZkKzsHoWqJNzGxCzlGTSZkfOei0QD2S3bGfqSMJMPxuvgHhUJotNgh3hGDYD01n56JiZy32JygaHHDLQbGWtkbFJy5BrMP5s6eL6V8Kcft71CxHZUMEEJ2LbYExYtPxaWuQ9USUCxbt7wTIjALoLN6aHW0GovD5euXWsYuOsqvyGuzJqjaohM9FFNmMz7ul0R4HxzTWWQqCZ8hp6O2yipRTs5k4RmGCTLfnYYdsadsadsadddddddddddddddddddddddddddd2111111111dsadsa/ – Dezso Gabos Sep 17 '15 at 10:59
  • @DezsoGabos: Sorry, `/` must be removed from character class. See my edit – Toto Sep 17 '15 at 18:53
0

You need to use a comma to separate minimum and maximum values in the limiting quantifier (otherwise, {1-4095} matches literally the sequence of characters {1-4095}):

/^[a-z0-9\s_@/.-]{1,4095}$/i

Also, the hyphen must be at the end in order to avoid escaping it and match a literal hyphen.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thanks. I still need a solution to limit the folder name size in the filepath to 255. – Dezso Gabos Sep 16 '15 at 06:32
  • Could you provide a sample string that will illustrate what exactly you need? Now, I think you try to validate both the filepath and the folder name inside it. Too cumbersome, that is why I'd like to see real-life examples. – Wiktor Stribiżew Sep 16 '15 at 06:48
  • /whatever/thisisnotapornstash/StillNot255CharactersButTheNextFolderIs/BPLrmwQRjmtwIGEMDcgGk1BCRY6ZkKzsHoWqJNzGxCzlGTSZkfOei0QD2S3bGfqSMJMPxuvgHhUJotNgh3hGDYD01n56JiZy32JygaHHDLQbGWtkbFJy5BrMP5s6eL6V8Kcft71CxHZUMEEJ2LbYExYtPxaWuQ9USUCxbt7wTIjALoLN6aHW0GovD5euXWsYuOsqvyGuzJqjaohM9FFNmMz7ul0R4HxzTWWQqCZ8hp6O2yipRTs5k4RmGCTLfnY/ – Dezso Gabos Sep 16 '15 at 14:04
  • No, please add this to the question itself, not as a comment, and describe what you need to do with this string, which part you need to check for length and how. Right now, it is too unclear. – Wiktor Stribiżew Sep 16 '15 at 14:07
  • The problem is, that folders that have names longer than 255 characters will not be created. In my case, I have an input field in the GUI, and want to do the validation on the frontend. You are right, the filepath and folder name should be done in two separate places. I will use two directives: data-ng-maxlength="4095" and one with data-ng-pattern (with a regex that limits my maximum foldername length. And this is where I need help). – Dezso Gabos Sep 16 '15 at 14:12
  • Please update the question with relevant data, and what you have come up to so far. I will see how I can help then. – Wiktor Stribiżew Sep 16 '15 at 14:14