All perfect praise be to Allah, The Lord of the Worlds. I testify that there is none worthy of worship except Allah, and that Muhammad is His slave and Messenger.
Saying ‘if only’ is not always prohibited, as Imaam Al-Bukhari dedicated a chapter to ‘if only’ being permissible in some contexts, and he mentioned a number of Ahaadeeth about this. As regards the prohibition in the Hadeeth narrated by Muslim, it means that it is prohibited to use it in relation to expressing sorrowful regret for worldly matters or objecting to the Predestination.
Ibn Daqeeq Al-‘Eed states in Ihkaam Al-Ahkaam Sharh ‘Umdat Al-Ahkaam: "The Prophet said: “If I had known beforehand about my affair what I came to know later, I would not have brought the Hadi (the sacrificial animal) with me (i.e. I would have taken off my Ihraam with those who have put their Ihraam after performing ‘Umrah.)” There are two matters involved here: One of them is the permissibility of using ‘if only’ in some contexts, even though some injunctions state otherwise - namely, the saying of the Prophet : “‘if only’ opens the door for the devil…” In trying to reconcile the two Ahaadeeth, scholars said that it is disliked to use ['if only'] in relation to expressing sorrowful regret for worldly matters, whether it means wishing to get something which one has missed, like saying “If only I had done such and such, I would have got such and such”, or wishing to have escaped something with which one has been afflicted, like when a person says “If only such and such had happened, such and such would not have happened to me”, as this contradicts having trust (in Allaah) through ascribing events to the Predestination and Decree of Allaah. However, if the term ‘if only’ is used in wishing to do righteous deeds that bring one closer to Allaah, as in the above Hadeeth, then it is not disliked......." [End quote]
Allaah Knows best.