ENOfficial
‘Ali reported God’s messenger as saying, “Good is the man learned in religion who, if people have need of him benefits them, but who, if they ignore him enriches himself.”* Razin transmitted it. *i.e. with religious exercises and study.
Source: sunnah.com
Other translations
Arabic
وَعَنْ عَلِيٍّ رَضِيَ اللَّهُ عَنْهُ قَالَ: قَالَ رَسُولُ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ: «نِعْمَ الرَّجُلُ الْفَقِيهُ فِي الدِّينِ إِنِ احْتِيجَ إِلَيْهِ نَفَعَ وَإِنِ اسْتُغْنِيَ عَنْهُ أَغْنَى نَفْسَهُ» . رَوَاهُ رزين
sunnah.com