The Robins had been second best throughout and trailed through Ben Knight’s effort, only for McGregor to be fed by fellow substitute Billy Bodin and fire a precise first-time finish into the far corner.
The hosts had dominated the chances prior to that, with Adam Mayor firing into the side-netting early on.
Cambridge got a deserved opener after 21 minutes when Mayor found James Gibbons, whose centre was swept home by Knight.
Knight was found in an almost identical position by a Louis Appere delivery, but this time he could only fire well over.
He then turned provider for James Brophy, whose low shot was kept out by a sprawling Conor Ripley save.
Knight should have made it 2-0 nine minutes after half-time as he surged past multiple challenges before being denied by Ripley.
Mamadou Jobe stabbed a Brophy corner over before the hosts were made to pay for their missed chances at the death.
