我需要從文本中洗掉標點符號:
data <- "Type the command AT&W enter. in order to save the new protocol on modem;"
gsub('[[:punct:] ] ',' ',data)
該解決方案給出了結果
[1] "Type the command AT W enter in order to save the new protocol on modem "
這不是想要的結果,因為我想保存&,因此:
[1] "Type the command AT&W enter in order to save the new protocol on modem "
uj5u.com熱心網友回復:
您可以嘗試用戶定義的正則運算式,其中包含任何不是 $ 或字母數字的內容:
data <- "Type the command AT&W enter. in order to save the new protocol on modem;"
gsub('[^&[:alnum:] ] ',' ',data)
uj5u.com熱心網友回復:
做反了怎么辦?&即用空字串替換不是字母、數字或 a 的所有內容:
gsub("[^[:alnum:][:space:]&]", "", data)
# [1] "Type the command AT&W enter in order to save the new protocol on modem"
uj5u.com熱心網友回復:
這是另一個正則運算式,它的字面意思是“找到除&”之外的所有標點符號。
gsub("[^\\P{P}&]", "", data, perl = T)
[1] "Type the command AT&W enter in order to save the new protocol on modem"
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/451869.html
下一篇:正則運算式-帶空格的組字串
