How do I add something after each number in a line in PYTHON?
2 answers
Given the comment you wrote, this regex should work:
import re
str = 'A = (x+2.)*(y+3.)-1'
print re.sub(r'(\d+)\.?',r'\1.0e0',str)
Output:
A = (x+2.0e0)*(y+3.0e0)-1.0e0
Regexp explanation:
-
(...)
- means a capture group that you need to capture for reuse during replacement -
\d
- means any number equivalent to [0-9] -
+
- means 1 or more events equivalent{1,}
-
\.?
- means we want either 0 or 1dot
.?
equivalent to{0,1}
Instead:
-
\1
- means that we want to take the first group of captured and insert it here
+6
source to share