简体   繁体   中英

how to remove special characters and number patterns from a string in R

I have a string "<U+7F85><U+934F><U+6DC7> <U+2730> Sascha Banks"

I want to exclude everything except the name "Sacha Banks" .

I perform:

name1<-c("<U+7F85><U+934F><U+6DC7> <U+2730> Sascha Banks ")
name2<-str_replace_all(name1, "[^[:alnum:]]", " ")

Actual Output: " U 7F85 U 934F U 6DC7 U 2730 Sascha Banks "

Expected Output: " Sascha Banks "

Please correct me.

Try

x <- "<U+7F85><U+934F><U+6DC7> <U+2730> Sascha Banks"
gsub("(<.*>)", "", x)
## [1] " Sascha Banks"

Try

gsub("<[^>]*>", "", name1)
## [1] "  Sascha Banks "

If you don't care to learn the regex this is a pretty straight forward approach that removes all angle brackets:

library(qdap)
bracketX("<U+7F85><U+934F><U+6DC7> <U+2730> Sascha Banks", "angle")

## [1] "Sascha Banks"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM