I tried to adapt the answer to Automatically add a final period if argument of '\paragraph' does not end with a punctuation character as a way to automatically add a period to a caption when there is none. For reasons I don't understand, this does not work out of the box. What is wrong (see Fig. 3)?
\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}
\DeclareCaptionTextFormat{addpunct}{\maybeaddperiod{#1}}
\captionsetup{textformat=addpunct}
\ExplSyntaxOn
\NewDocumentCommand{\maybeaddperiod}{m}
{
\userninefourtwoninethree_maybeaddperiod:n { #1 }
}
\cs_new_protected:Nn \userninefourtwoninethree_maybeaddperiod:n
{
\regex_match:nnTF { .*[!?.:] \Z } { #1 } { #1 } { #1. }
}
\ExplSyntaxOff
\begin{document}
\begin{figure}[h]
\centering
\includegraphics[height=2cm]{example-image}
\caption{This is an image}
\end{figure}
\begin{figure}[h]
\centering
\includegraphics[height=2cm]{example-image-a}
\caption{This is image A}
\end{figure}
\begin{figure}[h]
\centering
\includegraphics[height=2cm]{example-image-b}
\caption{This is image B.}
\end{figure}
\end{document}


\maybeaddperiod{Text!}produces Text!. – user94293 May 15 '23 at 21:49!.and?.pair from the argument by very similar trick. I leave this as an exercise for the reader. – wipet May 16 '23 at 03:44