The following Python script: re.sub("[^a-zA-Z]pi[^a-zA-Z]", "(math.pi)", "2pi3 + supirse")
results in: '(math.pi) + supirse'
While the match of a non-alpha before and after pi is critical, I do not want these non-alpha characters to be replaced in the match. I would like to see the following output: '2(math.pi)3 + supirse'
Note: A previous suggestion of the following: re.sub("\Bpi\B", "(math.pi)", "2pi3 + supirse")
results in a complete replacement of every instance: '2(math.pi)3 + su(math.pi)rse' which is also NOT what I am looking for
Use this instead:
re.sub("(?<=[^a-zA-Z])pi(?=[^a-zA-Z])", "(math.pi)", "2pi3 + supirse")Visualization: http://regex101.com/r/fX5wX3