我喜欢
stringr::str_extract
用于从字符串中提取模式。这使您可以简单地输入所需的模式,而不是试图替换其他所有内容:
mystring = c("Bloggs, Joe GBR London (1)/Bloggs, Joe London (2)",
"Bloggs, Joe London (1)/Bloggs, Joe GBR London (2)" ,
"Bloggs, Joe London (1)/Bloggs, Joe London (2)" ,
"Bloggs, Joe GBR London (1)/Bloggs, Joe GBR London (2)",
"Bloggs, J-S GBR London (1)/Bloggs, J-S GBR London (2)"
)
## extract first matches
stringr::str_extract(mystring, "[A-Z]{3}")
# [1] "GBR" "GBR" NA "GBR" "GBR"
## or get all matches with `str_extract_all`
stringr::str_extract_all(mystring, "[A-Z]{3}")
# [[1]]
# [1] "GBR"
#
# [[2]]
# [1] "GBR"
#
# [[3]]
# character(0)
#
# [[4]]
# [1] "GBR" "GBR"
#
# [[5]]
# [1] "GBR" "GBR"
可以在基本R中使用
substring
或
regmatches
和
regexpr
as seen in answers here
.