我想替換以美元符號開頭的字串的所有元素。在這里你可以找到我的例子:
my_string <- "$AAPL $TSLA I recomend to buy $TLSA and to sell $AAPL; the price is rising to $12.56"
替換是這個詞"cashtag"。
這是我想要的輸出:
my_string
"cashtag cashtag I recomend to buy cashtag and to sell cashtag; the price is rising to $12.56"
我已經用str_replace_alland試過了startsWith。
但到目前為止,我還沒有找到解決辦法。
我也很高興使用 tidyverse 包的建議解決方案。
uj5u.com熱心網友回復:
請注意,在 R 中的正則運算式中,您必須$使用反斜杠轉義,然后您必須使用另一個反斜杠轉義此反斜杠;)
stringr::str_replace_all(my_string, "\\$[A-Z] ", "cashtag")
[1] "cashtag cashtag I recomend to buy cashtag and to sell cashtag; the price is rising to $12.56"
uj5u.com熱心網友回復:
使用gsub:
gsub("\\$[A-Z] ", "cashtag", my_string)
# [1] "cashtag cashtag I recomend to buy cashtag and to sell cashtag; the price is rising to $12.56"
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/358687.html
