Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
特殊符号模式搜索与str_检测_R_Regex_Text_Dplyr_Stringr - Fatal编程技术网

特殊符号模式搜索与str_检测

特殊符号模式搜索与str_检测,r,regex,text,dplyr,stringr,R,Regex,Text,Dplyr,Stringr,假设我有以下df: library(dplyr) library(stringr) input <- data.frame( Id = c(1:6), text = c("(714.4) (714) (714*)", "(714.33)", "(189) (1938.23)", "(714.93+) (714*)", "(719)", "(718.4)")) 库(dplyr) 图书馆(stringr) 输入我们可以转义( 与OP的预期输出进行比较 Output # Id

假设我有以下df:

library(dplyr)
library(stringr)

input <- data.frame(
Id = c(1:6),
text = c("(714.4) (714) (714*)", "(714.33)", "(189) (1938.23)", "(714.93+) (714*)", "(719)", "(718.4)"))
库(dplyr)
图书馆(stringr)
输入我们可以转义

与OP的预期输出进行比较

Output
#  Id                 text first_match second_match
#1  1 (714.4) (714) (714*)           1            1
#2  2             (714.33)           0            1
#3  3      (189) (1938.23)           0            0
#4  4     (714.93+) (714*)           0            1
#5  5          (719) (299)           1            1
#6  6              (718.4)           0            0


在OP的代码中,第一个不起作用,因为
是一个元字符,在第二次尝试中,
|
被认为是固定的

你说得对。我是盲人,没有意识到括号也是一个特殊字符。但是,我有一个问题,你为什么要添加“+”str_detect函数开头的符号?@torakxkz只是一种将逻辑强制为二进制的方法。您也可以使用
作为.integer
。但是
ifelse
这样做有点牵强,因为真/假代表1/0
attempt_1 <- input %>%
  mutate(first_match = ifelse(str_detect(text, "(714)|(719)|(718)"), 1, 0), 
         second_match = ifelse(str_detect(text, "(714\\.33)|(714\\*)|(719)"), 1, 0))

attempt_2 <- input %>%
 mutate(first_match = ifelse(str_detect(text, fixed("(714)|(719)")), 1, 0), 
        second_match = ifelse(str_detect(text, "(714\\.33)|(714\\*)"), 1, 0))
library(dplyr)
library(stringr)
input %>%
    mutate(first_match = +(str_detect(text, "\\(714\\)|\\(719\\)")),
        second_match = +(str_detect(text, "\\(714\\.33\\)|\\(714\\*\\)|\\(719\\)")))
#   Id                 text first_match second_match
#1  1 (714.4) (714) (714*)           1            1
#2  2             (714.33)           0            1
#3  3      (189) (1938.23)           0            0
#4  4     (714.93+) (714*)           0            1
#5  5                (719)           1            1
#6  6              (718.4)           0            0
Output
#  Id                 text first_match second_match
#1  1 (714.4) (714) (714*)           1            1
#2  2             (714.33)           0            1
#3  3      (189) (1938.23)           0            0
#4  4     (714.93+) (714*)           0            1
#5  5          (719) (299)           1            1
#6  6              (718.4)           0            0