If a person misses out on something, one of two scenarios must apply. He may have felt helpless, which is the work of the shaytaan, who prompted him to say “If only”, but there is no benefit in that; rather it is the key to blame.
The second scenario is studying and pondering the divine decree; if something is decreed it could never miss him, and no one could get it before him. The Prophet (peace and blessings of Allah be upon him) taught us that which will benefit a person whether he gets what he wanted or not. He forbade us to say “if only” and told us that this opens the door to the shaytaan, because it implies regret for what has passed, grief, sorrow, and blaming the divine decree, and one is sinning thereby. It is the work of the shaytaan. This does not apply only to the words “if only”, rather it applies to the feelings in the heart that accompany it, which are contrary to complete faith and open the door to the shaytaan.