【问题标题】:How to get the part of the string before "|"如何获取“|”之前的字符串部分
【发布时间】:2021-11-19 03:50:23
【问题描述】:

在我的网络抓取练习中,我想出了一种特殊形式的字符串 -

st<-c("Samsung Galaxy M21 2021 Edition (Charcoal black , 6GB RAM, 128GB Storage) | FHD+ sAMOLED | 6 Months Free Screen Replacement for Prime")

我想要"|"之前的字符串部分,即我想要输出为:

"三星 Galaxy M21 2021 版(炭黑色,6GB RAM,128GB 存储)”

我正在尝试s &lt;- st %&gt;% str_replace("|.*","") 但没有比“st”(上面定义)得到任何改进

【问题讨论】:

标签: r web-scraping substring str-replace


【解决方案1】:

你可以试试-

st<-c("Samsung Galaxy M21 2021 Edition (Charcoal black , 6GB RAM, 128GB Storage) | FHD+ sAMOLED | 6 Months Free Screen Replacement for Prime")

sub('\\s*\\|.*', '', st)
#[1] "Samsung Galaxy M21 2021 Edition (Charcoal black , 6GB RAM, 128GB Storage)"

【讨论】:

    猜你喜欢
    • 2021-02-03
    • 2019-10-05
    • 2013-03-28
    • 2016-09-05
    • 2018-04-14
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 2016-08-15
    相关资源
    最近更新 更多