How to split
try1[No Work] > textSplit = text.split(r',') includes "%,"
try2[Works with issue]>
try3[No Work]>
sep = ","
at only "),"
and exclude "%,"
?1 |
text = "METAL (3.25%,a/d=13/1),IT(2.31%,a/d=10/0),HEALTHCAREINDEX(1.32%,a/d=13/7)" |
try2[Works with issue]>
textSplit = text.split(r'),') omits ")" at the end .How to add ")"
back to the string.try3[No Work]>
textSplit = text.split(r'/(?<=[)],)/gm')
positive lookahead doesn't split only at "),"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import re import numpy as np def randomColor(x): rColor = list (np.random.random(size = 3 ) * 256 ) return f '<span style = "color:rColor">{x}</span>' def spanColor(text): textSplit = text.split(r '/(?<=[)],)/gm' ) print (text) print (textSplit) textSplit_lst = [randomColor(x) for x in textSplit] textSplit_str = "," .join(textSplit_lst) return textSplit_str def regex_func(): print ( "Start" ) regStr = "METAL (3.25%,a/d=13/1),IT(2.31%,a/d=10/0),HEALTHCAREINDEX(1.32%,a/d=13/7)" regLst = spanColor(regStr) print ( f "regLst = {regLst}" ) regex_func() |