OR processing and regex

Started by Neal Krawetz, September 21, 2024, 09:07:01 AM

Previous topic - Next topic

Neal Krawetz

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.

StarGeek

#1
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 -m (-ignoreMinorErrors) option or -f (-ForcePrint) option 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.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Neal Krawetz

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!

StarGeek

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.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype