Matthew 1:3
Judah begot Perez and Zerah by Tamar, Perez begot Hezron, and Hezron begot Ram.
Verses three and four start the line of Judah.
The inclusion of five women, Tamar - was a Cannanite woman who posed as a prostitute to seduce Judah, Rahab - was a Gentile and a prostitute (v. 5; Jos 2:1), Ruth - a Moabite woman and a worshpper of idols (v.5; Ruth 2:11-12), Solomon's mother Bathsheba - Uriah's wife who committed adultry with David (v.6; 2 Sa 11-12), and Mary - who bore the stigma of pregnancy outside of wedlock is unusual but not without precedent in genealogies. All of these women had questionable backgrounds. Normally women did not appear in genealogies of the Old Testament. Four of these women were Gentiles, which was also unusual (two Canaanites, one Moabite, one Hittite Bathsheba). This shows and demonstrates the devine grace of God. Each of these women is an object lesson about the workings of divine grace.
We find Tamar's story in Genesis 38. Tamar is in the Bible because she sinned. Tamar, the Gentile, married the oldest son of Judah, who was named Er. He died suddenly because of sin. Judah asked his next son, Onan, to marry her. God killed him because he committed coitus interruptus. When Judah tried to save his last son, Shelah, by delaying giving Tamar to him, she took matters into her own hands by disguising herself as a prostitute and having sexual relations with Judah. When Judah discovered her pregnancy, he declared her more righteous than himself since he withheld Shelah from her. Out of that union the twins Perez and Zerah were born. Through this she became an ancestress of David.