Home > Blockchain >  JAVA - Regex to match different file name in folder
JAVA - Regex to match different file name in folder

Time:11-15

I have a folder with lots of images. All images are either type A or B, the only way to determine the type is by their name.

Type A Images -> 
12345-YZ.jpg or 232323-XZ.jpeg 
Type B images -> 
1231-WEAR-EWEF-DWW-ADAD.jpg or 345345-UWHDJ-asda-hshdshd.jpeg

What regex should I use to segregate the images??

Type A will always have one dash and then a few random characters. whereas type B it may have many words separated by a dash.

CodePudding user response:

You don't have to use regular expressions, you just need to differentiate by length.

Judging the length is much simpler than judging regular expressions

CodePudding user response:

So lets say the length of filenames cannot be an indicator of it's type, then

type A is:

^\d*-\w*\.jpe?g$
^\d*-\w*\.(jpg|jpeg)$

type B is:

^\d*-\w*(-\w*) \.jpe?g$
^\d*-\w*(-\w*) \.(jpg|jpeg)$

They are 2 of each but they mean the same thing, pick one depending if you prefer readable file format or the regex length.

  • Related