Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 351
Patrick Avatar asked Oct 23 '25 23:10

Patrick


2 Answers

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)
like image 97
Chris Ruehlemann Avatar answered Oct 25 '25 15:10

Chris Ruehlemann


A very short way uses sub, which will substitute the first open bracket ( with a space followed by an open bracket, i.e. (.

x <- '3.4(2.5-4.7)'
sub("\\(", " (", x)
# [1] "3.4 (2.5-4.7)"

Alternatively, you can specify the argument fixed = TRUE which considers the pattern as fixed and not as a regular expression.

x <- '3.4(2.5-4.7)'
sub("(", " (", x, fixed = TRUE)
# [1] "3.4 (2.5-4.7)"
like image 23
Ric S Avatar answered Oct 25 '25 17:10

Ric S