Search code examples
rregexstringgsub

How to remove square parentheses and text within from strings in R


I have encounter a problem in R language to process a data frame (test_dataframe) column (test_column) value like below:
Original strings in the column:

test_column
6.77[9]
5.92[10]
2.98[103]

I need to remove square brackets and any character inside square brackets, so the target value is below:

test_column
6.77
5.92
2.98

I tried with gsub function in R language, but not very lucky to resolve it, could someone help to figure out ?


Solution

  • I would use:

    input <- c("6.77[9]", "5.92[10]", "2.98[103]")
    gsub("\\[.*?\\]", "", input)
    
    [1] "6.77" "5.92" "2.98"
    

    The regex pattern \[.*?\] should match any quoted terms in square brackets, and using gsub would tell R to replace all such terms.