[英]Use regex to remove all special characters from string using thymeleaf
[英]R - Use regex to remove all strings, special characters, and pattern ending element
假設我有一個字符向量ids
,如下所示:
ids <- c("367025001", "CT_341796001", "M13X01692-01", "13C025050901", "13C00699551")
我想搜索每個元素並刪除所有字母,所有特殊字符,並在結束元素時刪除“01”。 所以ids
會變成:
ids_replaced <- c("3670250", "3417960", "1301692", "130250509", "1300699551")
我有點接近,但它沒有按照我的意圖行事。
gsub("(.*?)(\\d+?)(01$)", "\\2", ids, perl = TRUE)
你可以用
gsub("01$|\\D", "", ids)
# [1] "3670250" "3417960" "1301692" "130250509" "1300699551"
identical(gsub("01$|\\D", "", ids), ids_replaced)
# [1] TRUE
01
匹配“01” $
之前的可選\\n
和字符串的結尾 |
要么 \\D
匹配非數字(除了0-9之外) 使用rex可以使這種類型的任務更簡單一些。
ids <- c("367025001", "CT_341796001", "M13X01692-01", "13C025050901", "13C00699551")
re_substitutes(ids,
rex(non_digits %or% list("01", end)),
'',
global = TRUE)
#> [1] "3670250" "3417960" "1301692" "130250509" "1300699551"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.