ExifTool Forum

ExifTool => Bug Reports / Feature Requests => Topic started by: Neal Krawetz on September 21, 2024, 09:07:01 AM

Title: OR processing and regex
Post by: Neal Krawetz on September 21, 2024, 09:07:01 AM
I've been making some complex 'if' conditions with exiftool.
I've noticed a problem when using "or" with regex.

exiftool -File:FileName -EXIF:Make -EXIF:UserComment -PNG:Parameters -XMP:Provenance -XMP:DigitalSourceType -JUMBF:all -XMP:Credit \
-if '$XMP:Provenance or $XMP:DigitalSourceType or $JUMBF:all
This rule will print output if ANY of the 'or' conditions are matched. (Good!)

exiftool -File:FileName -EXIF:Make -EXIF:UserComment -PNG:Parameters -XMP:Provenance -XMP:DigitalSourceType -JUMBF:all -XMP:Credit \
-if '$XMP:Credit=~/\bAI\b/ or $EXIF:Make=~/\bAI\b/'
This will only match the first regex (XMP:Credit), not the second one (EXIF:Make).

If I swap them:
exiftool -File:FileName -EXIF:Make -EXIF:UserComment -PNG:Parameters -XMP:Provenance -XMP:DigitalSourceType -JUMBF:all -XMP:Credit \
-if '$EXIF:Make=~/\bAI\b/ or $XMP:Credit=~/\bAI\b/'
This will only match the first regex (EXIF:Make), not the second one (XMP:Credit).

This seems like a bug. The 'if' parser isn't handling the 'or' around regex correctly.

My current workaround is to split the if conditions around the regex:
exiftool -File:FileName -EXIF:Make -EXIF:UserComment -PNG:Parameters -XMP:Provenance -XMP:DigitalSourceType -JUMBF:all -XMP:Credit \
-if '$XMP:Provenance or $XMP:DigitalSourceType or $JUMBF:all' \
-execute -if '$XMP:Credit=~/\bAI\b/' \
-execute -if '$EXIF:Make=~/\bAI\b/' -common_args
This matches all of the 'or' conditions, but it will output the same thing twice if it matches multiple 'if' conditions.
Title: Re: OR processing and regex
Post by: StarGeek on September 21, 2024, 02:22:10 PM
This happens when one of the tags doesn't exist i.e. is undefined. The undefined tag cases a failure.

You can add a check for the existance of the tag like this
C:\>exiftool -G1 -a -s -make -Credit y:\!temp\Test3.jpg y:\!temp\Test4.jpg
======== y:/!temp/Test3.jpg
[XMP-photoshop] Credit                          : asdf AI werf
======== y:/!temp/Test4.jpg
[IFD0]          Make                            : asdf AI werf
    2 image files read

C:\>exiftool -G1 -a -s -if "$XMP:Credit=~/\bAI\b/ or $EXIF:Make=~/\bAI\b/" -Make -credit y:\!temp\Test3.jpg y:\!temp\Test4.jpg
======== y:/!temp/Test3.jpg
[XMP-photoshop] Credit                          : asdf AI werf
    1 files failed condition
    1 image files read

C:\>exiftool -G1 -a -s -if "($XMP:Credit and $XMP:Credit=~/\bAI\b/) or ($EXIF:Make and $EXIF:Make=~/\bAI\b/)" -Make -credit y:\!temp\Test3.jpg y:\!temp\Test4.jpg
======== y:/!temp/Test3.jpg
[XMP-photoshop] Credit                          : asdf AI werf
======== y:/!temp/Test4.jpg
[IFD0]          Make                            : asdf AI werf

Or use the -i (-Ignore) option (https://exiftool.org/exiftool_pod.html#i-DIR--ignore) -m (-ignoreMinorErrors) option (https://exiftool.org/exiftool_pod.html#m--ignoreMinorErrors) or -f (-ForcePrint) option (https://exiftool.org/exiftool_pod.html#f--forcePrint) which makes sure the tag exists.

There was a long-standing bug in the Windows version that when an undefined tag was used in an -if, then it would have the default of an empty string. This was fixed in version 12.41 (https://exiftool.org/ancient_history.html#v12.41).
Title: Re: OR processing and regex
Post by: Neal Krawetz on September 21, 2024, 04:22:41 PM
The bug makes sense.
-i and -f won't work for me, since many of my files don't have any of these tags. (-f always prints, even if it doesn't match.)

I really like your last example that uses '(var and var=~regex)'. Makes sense and removes the duplicates. Thank you!
Title: Re: OR processing and regex
Post by: StarGeek on September 21, 2024, 04:43:46 PM
Ooops, I didn't pay attention when copy/pasting from my clipboard history, I meant the -m option not the -i option. Fixed in original post.