Removing/replacing brackets from R string using gsub
Using the correct regex works:
gsub("[()]", "", "(abc)")
The additional square brackets mean "match any of the characters inside".
Remove square brackets from a string vector
You could gsub out the brackets like so:
n = c("[Dave]", "[Tony]", "[Sara]")
gsub("\\[|\\]", "", n)
[1] "Dave" "Tony" "Sara"
Using Gsub in R to remove a string containing brackets
We can by placing the brackets inside the square brackets as ()
is a metacharacter
gsub('[(]4:4aCO[)]','', '(5:3)(4:4)(5:3)(4:4)(4:4aCO)(6:2)_(4:4a)')
Or with fixed = TRUE
to evaluate the literal meaning of that character
gsub('(4:4aCO)','', '(5:3)(4:4)(5:3)(4:4)(4:4aCO)(6:2)_(4:4a)', fixed = TRUE)
Remove trailing brackets in a string
Another, relatively easy, regex solution is this:
data.frame(df) %>%
mutate(df = gsub("\\[\\W+|\\W+\\]", "", df))
df
1 Mamie Smith
2 Screamin' Jay Hawkins
Here we remove any non-alphanumeric character (\\W+
) occurring one or more times on the condition that it be preceded OR (|
) followed by a square bracket.
Alternatively, to borrow from @TaerJae but greatly simplified:
library(stringr)
data.frame(df) %>%
mutate(df = str_extract(df, '\\w.*\\w'))
Here we simply focus on the alphanumeric characters (\\w
) on either side of the string, while allowing for any characters (.*
) to occur in-between them thus capturing, for example, the apostrophe in Screamin'
and the whitespaces.
r - How can I remove a single pair of parentheses with text in a character?
Use sub
:
sub('\\(.*?\\)\\s', '', value)
#[1] "This is a (keep) test sentence."
()
are metacharacters and need to be escaped with\\
..*?
is to match as few characters possible till a closing bracket ()
) is encountered.
Removing parenthesis & dollar sign using gsub() in R?
I think the easiest way is to step through what is replaced. I'm inferring that you do not want to lose the negative-ness suggested by the parens, so we'll do two steps:
s <- c("($2,345)", "$3,500", "$5,600", "($3,234)")
gsub("[$),]", "", s)
# [1] "(2345" "3500" "5600" "(3234"
This removes most of the junk (that we did not want/need to keep), now let's deal with the leading left-paren:
gsub("^\\s*[(]", "-", gsub("[$),]", "", s))
# [1] "-2345" "3500" "5600" "-3234"
From here, if needed, you can convert to numeric
:
as.numeric(gsub("^\\s*[(]", "-", gsub("[$),]", "", s)))
# [1] -2345 3500 5600 -3234
This is not very robust to mal-formed strings. For instance, though I look for (and remove) leading space for the left-paren, anything else there will be a problem.
How to remove square brackets [] from R data frame without regex?
Somewhat mysterious, but
gsub("[][]","", x)
will do it (i.e., remove [
and ]
). The outer square brackets are interpreted as defining a set of characters; the inner square brackets are the set to remove. Putting the inner square brackets in the 'correct' order ([[]]
) doesn't work presumably because the inner []
is interpreted as part of a special like [:alpha:]
?
This regex does not work in stringr::str_remove()
("Missing closing bracket on a bracket expression"), but
stringr::str_remove_all(x,"[\\[\\]]")
does. (The double-backslashes are required because R uses single-backslash to identify special characters like newline (\n
) etc..)
In principle it should be possible to use the relatively new "raw strings" feature in R to simplify the expression (i.e. use regex pattern r"[\[\]]"
instead of "[\\[\\]]"
, but I seem to be discovering lots of weird interactions between that and the regex machinery (unless I'm doing something wrong/being sloppy, which is always a possibility ...)
If you really don't want to use regular expressions then
gsub(fixed = TRUE, "[", "",
gsub(fixed = TRUE, "]", "", x)
)
or
x |> str_remove_all(fixed("[")) |> str_remove_all(fixed("]"))
should work.
R - Clear brackets and anything inside it with gsub
s1<-as.character("Norway(19)")
s1<-gsub("\\(.*\\)", "", s1)
should get you there, .
is the wildcard in regex and *
repeats something 0 or more times.
Related Topics
R - Error When Using Geturl from Curl After Site Was Changed
R Eps Export and Import into Word 2010
How to Get The R Shiny Downloadhandler Filename to Work
Run R Interactively from Rscript
Existing Function to Combine Standard Deviations in R
Visualizing Distance Between Nodes According to Weights - with R
How to Create a Continuous Legend (Color Bar Style) for Scale_Alpha
Using Inst/Extdata with Vignette During Package Checking R 2.14.0
Creating a Cumulative Step Graph in R
Count Number of Values in Row Using Dplyr
Tidyr: Multiple Unnesting with Varying Na Counts
Learning to Write Functions in R
Applying Function (Ks.Test) Between Two Data Frames Column-Wise in R
Calculate Percentages/Proportions of Values by Group Using Data.Table
Margins Between Plots in Grid.Arrange
What's The Difference Between [1], [1,], [,1], [[1]] for a Dataframe in R