Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove square brackets from a string vector

Tags:

string

regex

r

I have a character vector in which each element is enclosed in brackets. I want to remove the brackets and just have the string.

So I tried:

n = c("[Dave]", "[Tony]", "[Sara]")  paste("", n, "", sep="") 

Unfortunately, this doesn't work for some reason.

I've performed the same task before using this same code, and am not sure why it's not working this time.

I want to go from '[Dave]' to 'Dave'.

What am I doing wrong?

like image 212
ATMathew Avatar asked Aug 25 '11 19:08

ATMathew


People also ask

How do you remove square brackets from string?

Using strip() to Remove Brackets from the Beginning and End of Strings in Python. If your brackets are on the beginning and end of your string, you can also use the strip() function. The Python strip() function removes specified characters from the beginning and end of a string.

How do you remove all brackets from a string?

String test = "watching tv (at home)"; test = test. replaceAll("(",""); test = test. replaceAll(")","");

How do you remove square brackets from Apex strings?

String s = "[abcdefg]"; s = s. replaceAll(regex, "");

How do you remove square brackets from string in flutter?

String str = "Test Message (To Be removed)"; var test = str. replaceAll(RegExp('\\(. *? \\)'), ''); print(test);


1 Answers

You could gsub out the brackets like so:

n = c("[Dave]", "[Tony]", "[Sara]")  gsub("\\[|\\]", "", n) [1] "Dave" "Tony" "Sara" 
like image 154
danpelota Avatar answered Oct 09 '22 09:10

danpelota