且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从字符串中提取数字和名称 [r]

更新时间:2023-11-07 12:20:22

所以如果我理解正确的话,你想提取括号内的元素.

So if I understand correctly you want to extract the elements within parenthesis.

您可以首先使用 str_extract_all 提取这些元素,包括括号:

You can first extract those elements, including the parenthesis, using str_extract_all:

b1 <- str_extract_all(string = a, pattern = "\\(.*?\\)")
b1
# [[1]]
# [1] "(37)"           "(\"Person10\")"

既然 str_extract_all 返回一个列表,我们把它变成一个向量:

Since str_extract_all returns a list, let's turn it into a vector:

b2 <- unlist(b1)
b2
# [1] "(37)"           "(\"Person10\")"

最后,您可以使用str_sub去除括号(每个字符串的第一个和最后一个字符):

Last, you can remove the parenthesis (the first and last character of each string) using str_sub:

b3 <- str_sub(string = b2, start = 2L, end = -2L) 
b3
# [1] "37"           "\"Person10\""

关于正则表达式模式的一些评论:\\(\\) 是您的左括号和右括号..*? 表示任何字符串但不贪婪,否则你会得到一个从第一个 ( 到最后一个 ) 的长匹配.

A few comments about the regex pattern: \\( and \\) are your opening and closing parenthesis. .*? means any character string but without being greedy, otherwise you would get one long match from the first ( to the last ).