Reputation: 11
How to remove everything between a slash sign and first space after in r?
#Input:
#"David\adfghsrth333 McDonalds", "Alice Jennifer\dfgadfg555 Smith",
#Desired output:
#"David McDonalds", "Alice Jennifer Smith"
Upvotes: 0
Views: 125
Reputation: 1052
If you have your input in a character vector called strings
, you can remove the characters between the slash sign and the following space like this
gsub('\\\\\\w+', '', strings)
The regular expression used looks strange because the backslash is a special character in both R and in regex.
"\\\\"
matches \
; note that we need so many slashes because we need to escape the special character in both R and regex\\w+
only matches word characters (numbers, letters, and underscores) so it stops matching when a space is reachedUpvotes: 2