[R] How to remove square brackets, etc. from address strings?

Sarah Goslee sarah.goslee at gmail.com
Tue May 22 14:39:59 CEST 2012


Hi Sabina,

You've run into two characteristics of regular expressions:
[ ] are special characters
* is a greedy match
Reading an intro regular expression document will help with both of those.

Meanwhile:

> x <- "[Swidsinski, Alexander; Loening-Baucke, Vera; Lochs, Herbert] Charite Humboldt Univ, Innere Klin, D-10098 Berlin, Germany; [Hale, Laura P.] Duke Univ, Med Ctr, Dept Pathol, Durham, NC 27710 USA"
> x
[1] "[Swidsinski, Alexander; Loening-Baucke, Vera; Lochs, Herbert]
Charite Humboldt Univ, Innere Klin, D-10098 Berlin, Germany; [Hale,
Laura P.] Duke Univ, Med Ctr, Dept Pathol, Durham, NC 27710 USA"

> gsub("\\[.*?\\] ", "", x) # escape [ and ] and make * lazy instead of greedy
[1] "Charite Humboldt Univ, Innere Klin, D-10098 Berlin, Germany; Duke
Univ, Med Ctr, Dept Pathol, Durham, NC 27710 USA"

Sarah

On Tue, May 22, 2012 at 6:08 AM, Sabina Arndt <sabina.arndt at hotmail.de> wrote:
>
> Hello,
>
>
>
> I'd like to remove the individual pairs of square brackets along with
> their content - plus the space directly behind it - from address strings
>  such as this:
>
>
>  [Swidsinski, Alexander; Loening-Baucke, Vera; Lochs, Herbert] Charite
> Humboldt Univ, Innere Klin, D-10098 Berlin, Germany; [Hale, Laura P.]
> Duke Univ, Med Ctr, Dept Pathol, Durham, NC 27710 USA
>
>
>
>  I'd like get the following result:
>
>
>
>  Charite Humboldt Univ, Innere Klin, D-10098 Berlin, Germany; Duke Univ, Med Ctr, Dept Pathol, Durham, NC 27710 USA
>
>
>
> I tried
>
>
>  address = gsub("(.*)[(.*)]", "\\2", address)
>
>
>
> But this deletes everything from the first opening bracket to the last closing bracket and leaves only the very last address:
>
>
>  Duke Univ, Med Ctr, Dept Pathol, Durham, NC 27710 USA
>
>
>
> How can I remove only the individual pairs of square brackets along with their content?
>
>
>
> Thank you very much in advance!
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org



More information about the R-help mailing list