Captain Macheath is a fictional character who appears both in John Gay's The Beggar's Opera (1728), its sequel Polly (1777), and roughly 200 years later in Bertolt Brecht's The Threepenny Opera.
Macheath made his first appearance in John Gay's The Beggar's Opera as a chivalrous highwayman. He then appeared as a pirate in Gay's sequel.
He was probably inspired in part by Jack Sheppard who, like Macheath, escaped from prison and enjoyed the affections of a prostitute, and despised violence. His nemesis is Peachum who, in John Gay's original work, keeps an account book of unproductive thieves (something that Macheath himself does in Bertolt Brecht's work). Both characters can be understood as satires of Robert Walpole and Jonathan Wild.
Captain Macheath is the chief protagonist of the 1841 Victorian penny dreadful Captain Macheath by Pierce Egan the Younger.
In Bertolt Brecht's The Threepenny Opera, he is referred to as "Mack the Knife", and is the subject of the song of the same name. Whilst his character plays roughly the same role as in the work it is derived from, Macheath is a much less romantic character here, described as a cutthroat, rapist and seducer of underage girls.