regex - Replacing numbers R regular-expression -


i trying code html tagging tool code in r , having difficulty finding , replace numbers colored numbers.

i think following in right direction not sure do:

txt <- gsub("\\<[:digit:]\\>", paste0(num.start,"\\1",num.end) , txt) 

this not seem job. overall, numbers not part of words identified , replaced tags before , after numbers change color , defined num.start, num.end variables.

for example:

num.start <- '<span style="color: #990000"><b>' num.end   <- '</b></span>' 

so able feed in r code , have write html tags when appropriate.

rcode:

 txt <- "a <- 3945 ; b <- 3453*3942*a"  gsub("\\<[:digit:]\\>", paste0(num.start,"\\1",num.end) , txt)  [1] "a <- <span style="color: #990000"><b>3945</b></span> ; b <- <span style="color: #990000"><b>3453</b></span>*<span style="color: #990000"><b>3942</b></span>*a" 

the hope copy modified r code html editor such blog , of numbers color coded.

thanks assistance! francis

this job though not recommend using regular expressions html:

gsub("(\\d+)", paste0(num.start,"\\1",num.end) , txt) 

the result:

[1] "a <- <span style=\"color: #990000\"><b>3945</b></span> ; b <- <span style=\"color: #990000\"><b>3453</b></span>*<span style=\"color: #990000\"><b>3942</b></span>*a" 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -