preg_match 检查 "if reach" 字符串结尾,而不是 "match character at" 字符串结尾

preg_match to check "if reach" end of string, not "match character at" end of string

我 Google 并搜索了这个网站,但没有找到类似的问题,我认为这是一个罕见的问题:我写了这个 RegExp

#<img .+?/font_image/(.{4})\.gif.*?>#

在 preg_match() 中检查此模式

<img src=".../font_image/xxxx.gif" ...>

在文本中,但有时由于打字错误缺少“>”。

如何修改此 RegExp 以匹配 >end-of-string 情况(注意:不能使用$ 因为我不知道字符串末尾的最后一个字符是什么,所以我尝试了 #<img .+?/font_image/(.{4})\.gif.*?># 但它不起作用)?

只需将 >$ 放在由 | 分隔的组中。请注意,不要将 char class 用于此类 OR 操作,因为 char class 中的 $ 应该失去它的特殊含义。

<img .+?/font_image/(.{4})\.gif.*?(?:>|$)

DEMO