0

I have a program that transfers files from one folder to another (and does some manipulation on those files), I am trying to create a regex to exclude certain files based on file name and extension, this also needs to support multiple entries that users enter the exclude patterns they want like - exclude pattern: thumbs.db ; donotmove.xls ; *.ini;

I have a folder with the following c:\testfolder:

test.txt
test.exe
something.jpg
other.pdf

Above should be moved (could be any file) Below should not be moved (could be anything that the user specifies in config

thumbs.db
donotmove.xlsx

The goal is to exclude unwanted files like thumbs.db, and other files that the user will want to exclude (the code is already written, I am just having trouble with the regex itself).

I have been able to create a regex that will capture thumbs.db, but I have not been able to successfully add a negative lookahead into the regex.

The regex I have is:

^[Tt][Hh][Uu][Mm][Bb][Ss]\.[Dd][Bb]$

this captures thumbs.db exactly meaning if I have a file with this name "tthumbs.db" it should not capture the file name.

  1. first problem I have is not being able to use negative lookhead to exclude the string I have tried the following:

    ^(?![Tt][Hh][Uu][Mm][Bb][Ss].[Dd][Bb])$ ^(?![Tt][Hh][Uu][Mm][Bb][Ss]).(?![Dd][Bb])$ ^(?![Tt])(?![Hh])(?![Uu])(?![Mm])(?![Bb])(?![Ss])).(?![Dd])(?![Bb])$

  2. second problem I have is not being able to correctly use capturing group to capture multiple strings in one regex, I tried to add another capture group but adding does not capture anything

    ^([Tt][Hh][Uu][Mm][Bb][Ss].[Dd][Bb])([Tt][Ee][Ss][Tt].[Tt][Xx][Tt])$

thnx in advance.

MrTux
  • 32,350
  • 30
  • 109
  • 146
  • `^(?!(?:foo|bar|[Tt][Hh][Uu][Mm][Bb][Ss]\.[Dd][Bb]|etc)$)`? – Wiktor Stribiżew Feb 26 '18 at 08:28
  • `^(?!(?i:file1|file2|thumbs\.db|fileN)$)` - Why don't you use the 'i' (ignoring case) flag? – shadowsheep Feb 26 '18 at 08:37
  • in case the duplicates don't help you for the capturing part, you can do this (using case insensitive, negative lookaheads, alternation and escaping the dots): `^((?!(?:thumbs\.db$|test\.txt$)).*)` (i assume you wanted to capture the files that are not your reserved ones, was writing an answer as question got closed, i don't usually answer in comments) – Kaddath Feb 26 '18 at 08:43
  • Hi thank you very much, I did not see the duplicated question (sorry), I used @shadowsheep answer and ended with ^(?!(?i:thumbs\.db|text\.txt)$) which works great. thank you all :) – Tsvi Moshkovitz Feb 26 '18 at 08:59

0 Answers0