Search code examples
r

Using a gsub to remove a special character that isn't always in the same place


I have a string in which I wish to remove the special character and letters/numbers surrounding the special character. The string will not always be the same length.

myString
"a-a b-b cc-cc*"
"1-1 *bb-bb"
"aa-bb*"
"aa-aa *3b-3b"

I am trying to remove the special character and any any letters/numbers that surround it so it should then look like;

myString
"a-a b-b"
"1-1"
""
"aa-aa"

Solution

  • The base R answer is great - a tidyverse approach using stringr and some reduced regex would be:

    stringr::str_trim(stringr::str_remove_all(xx, "\\b\\w+-\\w+\\*|\\*\\w+-\\w+\\b"))
    
    # [1] "a-a b-b" "1-1"     ""        "aa-aa"