我在提取/
之前和之后的字符串时遇到麻烦。
x <- c("maximusa/b=5/1","maximusa/b=-4/1","maximusa/b=3/-2")
before_slash=sub(".*=(\\d+).*","\\1", x, perl = TRUE)
给予
"5" "maximusa/b=-4/1" "3"
然后
after_slash=sub("^.*\\/(d+)","\\1", x, perl = TRUE)
给予
"maximusa/b=5/1" "maximusa/b=-4/1" "maximusa/b=3/-2"
OTH,预期输出
斜线前5 -4 3
斜线后1 1 -2
如何获得预期的输出?
谢谢,
我想再添加一个条件以提取字符串
假设我们有这样的字符串。
与OP一样,我们如何提取带有+
的符号以及忽略括号? @mob当前解决方案给出
x <- c("maximusa/b=(5/+1)","maximusa/b=(-4/1)","maximusa/b=(+3/-2)")
after_slash=sub("^.*/(\\d+)","\\1", x, perl = TRUE)
> after_slash
[1] "maximusa/b=(5/+1)" "1)" "maximusa/b=(+3/-2)"
和
before_slash=sub(".*=(-?\\d+).*","\\1", x, perl = TRUE)
> before_slash
[1] "maximusa/b=(5/+1)" "maximusa/b=(-4/1)" "maximusa/b=(+3/-2)"
我尝试了一些但没有运气!
答案 0 :(得分:2)
一个问题是
after_slash=sub("^.*\\/(d+)","\\1", x, perl = TRUE)
应该是
after_slash=sub("^.*/(\\d+)","\\1", x, perl = TRUE)
还要捕获负整数,您将要使用
before_slash=sub(".*=(-?\\d+).*","\\1", x, perl = TRUE)
after_slash=sub("^.*/(-?\\d+)","\\1", x, perl = TRUE)
令牌-?
的意思是“ -
字符,0或1次”
答案 1 :(得分:1)
我们可以使用str_extract_all
来匹配-
(如果有)后跟一个或多个数字([0-9]+
),并将其类型更改为numeric
>
library(tidyverse)
map_dfc(str_extract_all(x, "-?[0-9]+"), as.numeric)
# A tibble: 2 x 3
# V1 V2 V3
# <dbl> <dbl> <dbl>
#1 5 -4 3
#2 1 1 -2
或者在使用read.table
获取子字符串之后,然后使用sub
,然后将sep
指定为/
,以创建两列数据。frame
read.table(text= sub(".*=", "", x), sep="/")
# V1 V2
#1 5 1
#2 -4 1
#3 3 -2
或者另一个选择是strsplit
sapply(strsplit(x, "[=/]"), `[`, 3:4)
如果OP的字符串也具有()
,则第一个选项应该可以正常工作,但是在第二个选项中,我们可以更改
x1 <- c("maximusa/b=(5/1)","maximusa/b=(-4/1)","maximusa/b=(3/-2)")
read.table(text= gsub(".*=|[()]", "", x1), sep="/")
# V1 V2
#1 5 1
#2 -4 1
#3 3 -2
答案 2 :(得分:0)
这也应该起作用。
matrix(as.numeric(unlist(strsplit(
gsub("(^\\w*\\/)(b=)(-?\\d)(\\/)(-?\\d$)", "\\3 \\5", x), " "))), 2)
# [,1] [,2] [,3]
# [1,] 5 -4 3
# [2,] 1 1 -2