Search code examples
rstringgsubregex-group

How to insert a white space before open bracket


I have a string 3.4(2.5-4.7), I want to insert a white space before the open bracket "(" so that the string becomes 3.4 (2.5-4.7).

Any idea how this could be done in R?


Solution

  • x <- "3.4(2.5-4.7)"
    sub("(.*)(?=\\()", "\\1 ", x, perl = T)
    [1] "3.4 (2.5-4.7)"
    

    This regex is based on lookahead: it creates one capturing group subsuming everything up until the lookahead, namely, the opening parenthesis (?=\\()), recalls it and inserts one whitespace after it in the replacement argument to sub (which is enough unless you have more than one such substitution per string, in which case gsubis needed). The argument perl = Tneeds to be added to enable the lookahead.

    EDIT:

    If you have a string like this:

    x <- "3.4(2.5to4.7)"
    

    the regex gets slightly more complex; the underlying idea though remains the same: you divide the string into different captruing groups (...), which you then recall using appropriate backreference in the replacement argument while adding the sought spaces:

    sub("(.*)(\\(\\d+\\.\\d+)(to)(\\d+\\.\\d+\\))", "\\1 \\2 \\3 \\4", x)
    [1] "3.4 (2.5 to 4.7)"
    

    EDIT2:

    x <- '3.4(2.5,4.7)'
    sub("(.*)(\\(\\d+\\.\\d+)(,)(\\d+\\.\\d+\\))", "\\1 \\2\\3 \\4", x)
    [1] "3.4 (2.5, 4.7)"
    

    EDIT3:

    x <- '3(2,4)'
    sub("(.*)(\\(\\d+)(,)(\\d+)", "\\1 \\2\\3 \\4", x)