preg_match仅当它有“of”单词或什么都没有时,才会匹配


preg_match matches only if it have "of" word or nothing

我有 2 个字符串

"years of exp" & "years exp".

我只想匹配上面的字符串。

但不想匹配字符串

"years sssss exp"
只有当"of"单词在"year"和"

exp"之间或"year"和"exp"之间没有任何匹配时,它才匹配。

谢谢

您可以使用

?量词,它的意思是"零或一"。

"years (of )?exp"