R中的正则表达式,第n次出现

时间:2016-01-02 22:36:56

标签: regex r

我正在阅读R中的文本文件,并希望每隔3次更换一次' |'用' \ n'这是我的代码和输入数据

**Input Data** 
======================
    'Monday, November 2, 2015|10:21:27|17:58:12|Tuesday, November 3, 2015|10:13:09|18:52:44|Wednesday, November 4, 2015|10:11:52|18:40:36|Thursday, November 5, 2015|10:31:42|18:16:57|Friday, November 6, 2015|10:13:13|--|Saturday, November 7, 2015|--|--|Sunday, November 8, 2015|--|--|Monday, November 9, 2015|--|--|Tuesday, November 10, 2015|10:03:20|18:07:52|Wednesday, November 11, 2015|09:40:20|18:42:20|Thursday, November 12, 2015|10:38:56|18:37:20|Friday, November 13, 2015|10:45:26|18:09:54|Saturday, November 14, 2015|--|--|Sunday, November 15, 2015|--|--|Monday, November 16, 2015|--|--|Tuesday, November 17, 2015|10:11:43|18:36:15|Wednesday, November 18, 2015|--|--|Thursday, November 19, 2015|--|--|Friday, November 20, 2015|12:14:25|20:25:08|Saturday, November 21, 2015|--|--|Sunday, November 22, 2015|--|--|Monday, November 23, 2015|10:08:08|17:57:35|Tuesday, November 24, 2015|14:30:32|--|'

**My R-Code**
====================

    emp <- readChar(FileDir, (file.info(FileDir)$size-172))
    emp <- gsub("\r\n","|",emp)
    empTMP <- gsub('([^|]*|[^|]*|[^|]*)|',"\1\n",emp)


**output**
====================
"\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n"

**Required output**
====================
Monday, November 2, 2015|10:21:27|17:58:12
Tuesday, November 3, 2015|10:13:09|18:52:44
Wednesday, November 4, 2015|10:11:52|18:40:36
Thursday, November 5, 2015|10:31:42|18:16:57
Friday, November 6, 2015|10:13:13|--
Saturday, November 7, 2015|--|--
Sunday, November 8, 2015|--|--
Monday, November 9, 2015|--|--
Tuesday, November 10, 2015|10:03:20|18:07:52
Wednesday, November 11, 2015|09:40:20|18:42:20
Thursday, November 12, 2015|10:38:56|18:37:20
Friday, November 13, 2015|10:45:26|18:09:54
Saturday, November 14, 2015|--|--
Sunday, November 15, 2015|--|--
Monday, November 16, 2015|--|--
Tuesday, November 17, 2015|10:11:43|18:36:15
Wednesday, November 18, 2015|--|--
Thursday, November 19, 2015|--|--
Friday, November 20, 2015|12:14:25|20:25:08
Saturday, November 21, 2015|--|--
Sunday, November 22, 2015|--|--
Monday, November 23, 2015|10:08:08|17:57:35
Tuesday, November 24, 2015|14:30:32|--

请帮助我做错了,我在文本编辑器中查看上面的正则表达式,但是在&#34; R&#34;它没有产生正确的结果。

2 个答案:

答案 0 :(得分:4)

以下作品:

#input <- #your input string

x <- strsplit(input, split = "|", fixed = TRUE)[[1L]]

idx <- seq(3L, length(x), by = 3L)
x[idx] <- paste0(x[idx], "\n")
x[-idx] <- paste0(x[-idx], "|")
paste(x, collapse = "")

或者在一个命令中:

paste(paste0(x <- strsplit(input, split = "|", fixed = TRUE)[[1L]],
             rep_len(c("|", "|", "\n"), length(x))), collapse = "")

如果你想坚持使用gsub,那么这也适用:

gsub("([^|]*\\|[^|]*\\|[^|]*)\\|", "\\1\n", input)

细分(regex101 colored version):

gsub(paste0("(",        #start capturing group 1
            "[^|]*",    #Matching anything but | 0 or more times
            "\\|",      #Match | (must escape because it's reserved for OR)
            "[^|]*\\|", #again
            "[^|]*",    #again matching anything but |
            ")",        #end captured group
            "\\|"),     #captured group is followed by a third |
     "\\1\n",input)     #replace match with captured group followed by \n
                        #  (instead of |)

(只是注意到你原来的尝试非常接近。只是你忘记了正确地逃避了事情:"\\1",而不是"\1",而"|"是保留的,所以我们必须逃避好吧。@ CAFEBABE也是对的,这似乎更适合awk ...)

答案 1 :(得分:1)

 empTMP <- gsub('([^|]*|[^|]*|[^|]*)|',"\1\n",emp)

这是引起麻烦的线路。

应该是

empTMP <- gsub('([^|]*|[^|]*|[^|]*)|',"\\1\n",emp)

(请注意\\1

侧面:为什么要将R用于此任务。看起来像标准shell脚本的东西 在方2:为什么teradata?你在哪个TD盒子上使用R?