I am looking for a pythonic way to replace substrings in a string, similar to re.sub, but with additional processing of the found text. It can probably be achieved with pure regular expression syntax, but it very quickly becomes unreadable, which is worse than simple—really hard to extend/debug.
This is what I need to achieve:
Input string: text1 (2, 100) text2 (34,23) text3
Output: Same string, but (2, 100) wrapped into an HTML code using values 12 and 14; same for (34, 23). Something like:
text1 <span data-coord='{"x": 0.02, "y": 1}'>(2, 100)</span>
text2 <span data-coord='{"x": 0.34, "y": 0.23}'>(34, 23)</span>
text3
Iteration through matches with re.finditer seems a logical solution, but how do I get the rest of the text?
EDIT: Numbers may be one- to three-digit ones, between 0 and 100.
FOOTNOTE: I'd really prefer to have a solution where the found groups for x and y are an input to my custom function, to have a complete freedom of what to do with the found groups. E.g. to do error processing: in case the number is outside the range of 0...100, I may want to highlight it with red. I am sure I can define that behaviour in terms of regex as well, but I find it wrong: regex is for text processing, not number manipulation. And it obscures the logic of the code.