str_enttract所有语法
我需要一些帮助Stringr :: str_extract_all
x
是我的数据框架的名称。
V1
(A_K9B,A_K9one,A_K9two,B_U10J)
x = x %>%
mutate(N_alph = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[A-Z]'), toString))
x = x %>%
mutate(N_.1 = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[o][n][e]'), toString))
x = x %>%
mutate(N_.2 = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[t][w][o]'), toString))
这是我当前的输出:
V1 N_alph N_.1 N_.2
(A_K9B,A_K9one,A_K9two,B_U10J) A_K9B A_K9one A_K9two
我的列n_alph
我都可以与其他两个分开。但理想情况下,我想避免键入[o] [n] [e]
和[t] [w] [o]比一个字母字母,如果我使用:
x = x %>%
mutate(N_alph = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[A-Z]'), toString))
x = x %>%
mutate(N_all.words = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[\\w+]'), toString))
输出为:
V1 N_alph N_all.words
(A_K9B,A_K9one,A_K9two,B_U10J) A_K9B A_K9B,A_K9o,A_K9t
所需的输出将为
V1 N_alph N_all.words
(A_K9B,A_K9one,A_K9two,B_U10J) A_K9B A_K9one,A_K9two
I need some help with stringr::str_extract_all
x
is the name of my data frame.
V1
(A_K9B,A_K9one,A_K9two,B_U10J)
x = x %>%
mutate(N_alph = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[A-Z]'), toString))
x = x %>%
mutate(N_.1 = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[o][n][e]'), toString))
x = x %>%
mutate(N_.2 = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[t][w][o]'), toString))
This is my current output:
V1 N_alph N_.1 N_.2
(A_K9B,A_K9one,A_K9two,B_U10J) A_K9B A_K9one A_K9two
I am fine with my column N_alph
as is I want it separate from the other two. But Ideally I would like to avoid typing [o][n][e]
and [t][w][o]
for those variables that are followed by words rather than one alphabetical letter, if I use:
x = x %>%
mutate(N_alph = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[A-Z]'), toString))
x = x %>%
mutate(N_all.words = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[\\w+]'), toString))
Output is:
V1 N_alph N_all.words
(A_K9B,A_K9one,A_K9two,B_U10J) A_K9B A_K9B,A_K9o,A_K9t
Desired output would be
V1 N_alph N_all.words
(A_K9B,A_K9one,A_K9two,B_U10J) A_K9B A_K9one,A_K9two
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用\ w,\ b,\ s等这样的元视频器时,您不需要方括号。但是,如果您确实使用了方括号,那么
+
需要在外面。另外,当我们谈论单个字符而不是字符的组合时,数字组应为[0-9]。要考虑到高于9的数字,我们只是扩展了使用{}括号或简单的+
运算符检查组的次数。最终结果看起来像是这样:结果:
我还创建了一个我发现了一些整理的版本:
When you use metacharacters like \w, \b, \s, etc., you don't need the square brackets. But if you do use the square brackets than the
+
would need to be outside. Also, the number group should be [0-9] as we are talking about individual characters, not combinations of characters. To take into account numbers higher than 9 we just expand the amount of times we check for the group with {} brackets, or simply the+
operator. The final result looks like so:Resulting to:
I also created a version that I found a little tidier: