How can I only return the regex match

Started by J4c06r1nw1s, January 09, 2022, 04:34:10 PM

Previous topic - Next topic

J4c06r1nw1s

How can I only return the regex match?

/volume1/footage/PHOTOS/IMG_1234.jpg
/volume1/footage/PHOTOS/DSCN1234.jpg
/volume1/footage/PHOTOS/_DSC12345.jpg
/volume1/footage/PHOTOS/P1234567.jpg
/volume1/footage/PHOTOS/S1234567.jpg


%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        MyFileNumber => {
            Desire => {
                0 => 'FileName',
                1 => 'PreservedFileName',
                2 => 'FileNumber',
                3 => 'ImageNumber',
                4 => 'ShutterCount'
            },
            ValueConv => q{
                my $FileName = $val[0];
                my $PreservedFileName = $val[1];
                my $FileNumber = $val[2];
                my $ImageNumber = $val[3];
                my $ShutterCount = $val[4];

                # Restore FileName from PreservedFileName Tag
                $FileName =~ s/$FileName/$PreservedFileName/ if $PreservedFileName;
               
                # Get FileNumber from tag
                $FileName =~ s/$FileName/$FileNumber/ if $FileNumber; # <-- This may return
                $FileName =~ s/$FileName/$ImageNumber/ if $ImageNumber; # <-- This may return
                $FileName =~ s/$FileName/$ShutterCount/ if $ShutterCount; # <-- This may return

                # Get FileNumber out of FileName
                $FileName =~ s/^IMG_(\d{4})\.[^.]*$/$1/i; # <-- This may return
                $FileName =~ s/^DSCN(\d{4})\.[^.]*$/$1/i; # <-- This may return
                $FileName =~ s/^_DSC(\d{5})\.[^.]*$/$1/i; # <-- This may return
               
                return $FileName ? $FileName : undef; # <-- This returns everything
            },
        },
    },
);
1;  #end



exiftool -config config -s -r '/volume1/footage/PHOTOS' -MyFileNumber
======== /volume1/footage/PHOTOS/IMG_1234.jpg
MyFileNumber                   : 1234
======== /volume1/footage/PHOTOS/DSCN1234.jpg
MyFileNumber                   : 1234
======== /volume1/footage/PHOTOS/_DSC12345.jpg
MyFileNumber                   : 12345
======== /volume1/footage/PHOTOS/P1234567.jpg
MyFileNumber                   : P1234567.jpg <-- How do I skip this one
======== /volume1/footage/PHOTOS/S1234567.jpg
MyFileNumber                   : S1234567.jpg <-- And how do I skip this one
Using ExifTool v12.37 on Linux

J4c06r1nw1s

#1
I found a way.

Are there any improvements?


%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
#------------------------------------------------------------------------------
        MyFileNumber => {
            Desire => {
                0 => 'FileName',
                1 => 'PreservedFileName',
            },
            ValueConv => q{
                # Restore Original FileName
                $val[0] = $val[1] if $val[1];

                # Strip off extention
                $val[0] =~ s/\.[^.]*$//;

                # Strip of combinations with: - copy kopie 1 (1)
                $val[0] =~ s/( - |)((k|c)op(y|ie))( (\(|)(\d+|)(\)|)|)//ig;

                if ( $val[0] =~ /^IMG_(\d{4})/ ) { return $1; }
                if ( $val[0] =~ /^DSCN(\d{4})/ ) { return $1; }
                if ( $val[0] =~ /^_DSC(\d{5})/ ) { return $1; }
               
                return undef;
            },
        },
#------------------------------------------------------------------------------
        MyCountNumber => {
            Desire => {
                0 => 'MyFileNumber6',
                1 => 'FileNumber',
                2 => 'ImageNumber',
                3 => 'ShutterCount',
            },
            ValueConv => '$val[0] || $val[1] || $val[2] || $val[3]',
            PrintConv => '$val',
        },
#------------------------------------------------------------------------------
    },
);
1;  #end
Using ExifTool v12.37 on Linux

Phil Harvey

The substitution expression will return true if it is successful.

I think you want something like this:

    $val =~ s/^IMG_(\d{4})\.[^.]*$/$1/i and return $val;

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

J4c06r1nw1s

Thank you Phil Harvey. For your ExifTool and quick responses to questions.

I found more problems like leftover spaces and dashes and now I have solved them.
And learned that my Nikon files contain more tags.
Thought it would be one or the other tag.(FileNumber, ImageNumber, ShutterCount)
But it could be more. And with a different value.
Had to change the order in MyCountNumber

exiftool -config config -s -r /volume1/footage/PHOTOS/TEST_FILES -FileNumber -ImageNumber -ShutterCount -MyFileNumber -MyCountNumber
======== /volume1/footage/PHOTOS/TEST_FILES/NIKON_D800_1234.DNG
FileNumber                      : 1031
ImageNumber                     : 19765
ShutterCount                    : 19765
MyFileNumber                    : 1234
MyCountNumber                   : 19765
======== /volume1/footage/PHOTOS/TEST_FILES/_DSC1234.DNG
FileNumber                      : 1031
ImageNumber                     : 19765
ShutterCount                    : 19765
MyFileNumber                    : 1234
MyCountNumber                   : 19765
======== /volume1/footage/PHOTOS/TEST_FILES/IMG_1234.dng
MyFileNumber                    : 1234
MyCountNumber                   : 1234
======== /volume1/footage/PHOTOS/TEST_FILES/IMG_123456.dng
MyFileNumber                    : 123456
MyCountNumber                   : 123456
======== /volume1/footage/PHOTOS/TEST_FILES/TEST_123456.dng
======== /volume1/footage/PHOTOS/TEST_FILES/IMG_1234 - kopie.dng
MyFileNumber                    : 1234
MyCountNumber                   : 1234
======== /volume1/footage/PHOTOS/TEST_FILES/IMG_1234 - kopie - copy 1 copy (2).dng
MyFileNumber                    : 1234
MyCountNumber                   : 1234
======== /volume1/footage/PHOTOS/TEST_FILES/dit is een test.dng
======== /volume1/footage/PHOTOS/TEST_FILES/NIKON_D300S_1234.NEF
FileNumber                      : 3839
ShutterCount                    : 56549
MyFileNumber                    : 1234
MyCountNumber                   : 56549
   11 directories scanned
    9 image files read


%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
#------------------------------------------------------------------------------
        MyFileNumber => {
            Desire => {
                0 => 'FileName',
                1 => 'PreservedFileName',
            },
            ValueConv => q{
                # Restore Original FileName
                $val[0] = $val[1] if $val[1];

                # Strip off extention
                $val[0] =~ s/\.[^.]*$//;

                # Strip of combinations with: - copy kopie 1 (1)
                $val[0] =~ s/( - |)((k|c)op(y|ie))( (\(|)(\d+|)(\)|)|)//ig;

                # Remove leftover spaces
                $val[0] =~ s/\s*$//;

                # Remove leftover dashes
                $val[0] =~ s/-*$//;

                # Find the numbers inside de FileName
                $val[0] =~ s/^IMG_(\d{4})/$1/i and return $val[0];
                $val[0] =~ s/^DSCN(\d{4})/$1/i and return $val[0];
                $val[0] =~ s/^_DSC(\d{4})/$1/i and return $val[0];
                $val[0] =~ s/^NIKON_D300S_(\d{4})/$1/i and return $val[0];
                $val[0] =~ s/^NIKON_D800_(\d{4})/$1/i and return $val[0];
               
                return undef;
            },
        },
#------------------------------------------------------------------------------
        MyCountNumber => {
            Desire => {
                0 => 'ShutterCount',
                1 => 'ImageNumber',
                2 => 'FileNumber',
                3 => 'MyFileNumber',
            },
            ValueConv => '$val[0] || $val[1] || $val[2] || $val[3]',
            PrintConv => '$val',
        },
#------------------------------------------------------------------------------
    },
);
1;  #end


I think I am quite satisfied. Maybe I'll see if I can combine this regex into one rule.

# Strip of combinations with: - copy kopie 1 (1)
$val[0] =~ s/( - |)((k|c)op(y|ie))( (\(|)(\d+|)(\)|)|)//ig;

# Remove leftover spaces
$val[0] =~ s/\s*$//;

# Remove leftover dashes
$val[0] =~ s/-*$//;
Using ExifTool v12.37 on Linux

Phil Harvey

Quote from: J4c06r1nw1s on January 10, 2022, 08:30:37 AM

# Strip of combinations with: - copy kopie 1 (1)
$val[0] =~ s/( - |)((k|c)op(y|ie))( (\(|)(\d+|)(\)|)|)//ig;

# Remove leftover spaces
$val[0] =~ s/\s*$//;

# Remove leftover dashes
$val[0] =~ s/-*$//;


Instead of doing this sort of thing ( - |) I would suggest doing ( - )?

The last 2 substitutions can be done like this:  $val[0] =~ s/[- ]+$//;

Note that doing using * to match zero or more characters doesn't make sense when you are trying to remove them (you can't remove 0 characters, so it is faster if the match fails in this case).  So instead I used + to match 1 or more characters.

- Phil

...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).