regexexcelvbapattern-matchingmultiple-matches

Regular Expression Pattern for Full Match of Filenames


I am looping through an Array of Image Filenames which are as below:

  1. ConImage1 - Core.png
  2. ConImage211 - Core.png
  3. ConImage34 - Core.png
  4. ConImage09 - Core.png
  5. ConImage11 - Core.png
  6. ConImageOri23.png
  7. ConImageOri2.png
  8. ConImageOri11.png
  9. ConImageOri132.png
  10. ConImageForEng7 - Core.png
  11. ConImageForEng12 - Core.png
  12. ConImageForEng11 - Core.png
  13. ConImageForEng10 - Core.png
  14. ConImage1-Dislikes-Core.png
  15. ConImage1-Likes-Core.png
  16. ConImage12 - Dislikes - Core.png
  17. ConImage12 - Likes - Core.png
  18. ConImage34 - Dislikes - Core.png
  19. ConImage34 - Likes - Core.png
  20. ConImage55 - Dislikes - Core.png
  21. ConImage55 - Likes - Core.png

I need a Regex pattern that will only match the following Filenames:

  1. ConImage1 - Core.png
  2. ConImage211 - Core.png
  3. ConImage34 - Core.png
  4. ConImage09 - Core.png
  5. ConImage11 - Core.png

  6. ConImageOri23.png

  7. ConImageOri2.png
  8. ConImageOri11.png
  9. ConImageOri132.png

and exclude the filenames containing the words : Likes, Dislikes, ForEng etc.

Edit:

I am not good at regex but trying something like this to limit my search.

^(ConImage|(Orig|!ForEng)){1}[0-9\ \-]+[\W(Dislikes|Likes)][0-9\ \-]+(Core\.png)$/i/g

OR the below works for finding necessary files, but if some odd name comes out e.g. ConImageMaxx12 - Core.png etc, it also picks that up.

^ConImage((?!ForEng|Dislikes|Likes).)*$

I don't want to implement VBA.Filter function but need a Regex solution.

Any help would be most appreciated.


Solution

  • Without specific rules, it is hard to come up with a regex. To match the specific values you have posted (and exclude the ConImageMaxx12 you mentioned in a comment), you could try:

    Edit to correct the missing "Start of String" token

    ^ConImage(?:Ori)?\d+(?:(?!(?:For|Likes)).)*$
    

    That regex matches:

    Whether that set of rules is sufficient to eliminate the unwanted file names cannot be stated, since I am only guessing at some rules based on what you've written.

    Here is a more formal explanation of the Regex with some links to a tutorial explanation:

    Note that, in accordance with your comments, we set the case-insensitive option

    Match Specific File Names

    ^ConImage(?:Ori)?\d+(?:(?!(?:For|Likes)).)*$
    

    Options: Case insensitive; ^$ match at line breaks

    Created with RegexBuddy