Armchair graphene nanoribbons (AGNRs) known as semiconductors are holding promise for nanoelectronics applications and sparking increased research interest. Currently, synthesis of 5-AGNRs with a quasi-metallic gap has been achieved using perylene and its halogen-containing derivatives as precursors via on-surface synthesis on a metal substrate. However, challenges in controlling the polymerization and orientation between precursor molecules have led to side reactions and the formation of by-products, posing a significant issue in purity. Here a precision synthesis of confined 5-AGNRs using molecular-designed precursors without halogens is proposed to address these challenges. Perylene and its dimer quaterrylene are utilized for filling into single-walled carbon nanotubes (SWCNTs), following a precursor-driven transition into 5-AGNRs by heat-induced polymerization and cyclodehydrogenation. SWCNTs restrict the alignment of confined quaterrylene enabling their polymerization with a head-to-tail arrangement, which results in the formation of pure 5-AGNRs with three times higher yield than that of perylene, as the free rotation capability of perylene molecules inside SWCNTs lead to the formation of 5-AGNRs concomitant with by-products. This work provides a templated route for synthesizing desired GNRs based on molecular-designed precursors and confined polymerization, bringing advantages for their applications in electronics and optoelectronics.