Search code examples
rstringreplacelookuplookup-tables

r replace text within a string by lookup table


I already have tried to find a solutions on the internet for my problem, and I have the feeling I know all the small pieces but I am unable to put them together. I'm quite knew at programing so pleace be patient :D...

I have a (in reality much larger) text string which look like this:

string <- "Test test [438] test. Test 299, test [82]."

Now I want to replace the numbers in square brackets using a lookup table and get a new string back. There are other numbers in the text but I only want to change those in brackets and need to have them back in brackets.

lookup <- read.table(text = "
Number   orderedNbr
1 270 1
2 299 2
3 82  3
4 314 4
5 438 5", header = TRUE)

I have made a pattern to find the square brackets using regular expressions

pattern <- "\\[(\\d+)\\]"

Now I looked all around and tried sub/gsub, lapply, merge, str_replace, but I find myself unable to make it work... I don't know how to tell R! to look what's inside the brackets, to look for that same argument in the lookup table and give out what's standing in the next column.

I hope you can help me, and that it's not a really stupid question. Thx


Solution

  • We can use a regex look around to match only numbers that are inside a square bracket

    library(gsubfn)
    gsubfn("(?<=\\[)(\\d+)(?=\\])", setNames(as.list(lookup$orderedNbr), 
                 lookup$Number), string, perl = TRUE)
    #[1] "Test test [5] test. Test [3]."
    

    Or without regex lookaround by pasteing the square bracket on each column of 'lookup'

    gsubfn("(\\[\\d+\\])", setNames(as.list(paste0("[", lookup$orderedNbr, 
              "]")), paste0("[", lookup$Number, "]")), string)