正则表达式在R中找到特定模式

时间:2015-06-18 03:29:29

标签: regex r

我有一个如下数据集:

 dput(d1)
structure(list(FNUM = structure(1L, .Label = "20140824-0227", class = "factor"), 
    DESCRIPTION = "From : J LTo : feedback@lsd.goe.sfcc : Bcc : Sent On : Mon Apr 13 08:59:18 S 2015Subject : RE:Re: Suspect illegally modified vehiclesBody : Our Ref: BS-CT-1408-0665Date : 2-Apr-2015Our Ref: 2015/Jan/3224Date : 2-Apr-2015Thank you very much! Please conduct a thorough check on the vehicle other than the exhaust system. Warm regards,J L--------------------------------------------On Mon, 4/13/15, feedback@lsd.goe.sf <feedback@lsd.goe.sf> wrote: Subject: RE:Re: Suspect illegally modified vehicles To: jl1229@yahoo.ca Received: Monday, April 13, 2015, 8:56 AM  Our Ref: GCE/VS/VS/VE/F20.000.000/38104 Date : 8-Apr-2015 Tel : 1800 2255 582 Fax : 6553 5329  -------------------------------------------- On Mon, 4/6/15, feedback@lsd.goe.sf <feedback@lsd.goe.sf> wrote:   Subject: Suspect illegally modified vehicles  To: joa@dccs.ca  Received: Monday, April 6, 2015, 11:06 AM    Our Ref:  GCE/VS/VS/VE/F20.000.000/37661  Date : 2-Apr-2015  Tel : 1812 2235 582  Fax : 6553 5329        Dear Ms L     Our records show that the vehicle bearing registration"), .Names = c("FNUM", 
"DESCRIPTION"), row.names = "1", class = "data.frame")

我使用以下regex来识别值Our Ref:

> gsub(" *(Our Ref|Date) *:? *","",regmatches(d1[1,2],gregexpr("Our Ref *:[^:]+",d1[1,2]))[[1]])

[1] "BS-CT-1408-0665"                "2015/Jan/3224"                 
[3] "GCE/VS/VS/VE/F20.000.000/38104" "GCE/VS/VS/VE/F20.000.000/37661" 

但我只想要以Our Ref:开头的GCE值,如何将输出限制为以GCE开头的值。

期望的结果:

[1] "GCE/VS/VS/VE/F20.000.000/38104" "GCE/VS/VS/VE/F20.000.000/37661" 

已更新 对于问题的第二部分:

dput(d1)
structure(list(FNUM = structure(1L, .Label = "20140824-0227", class = "factor"), 
    DESCRIPTION = "From : J LTo : feedback@lsd.goe.sfcc : Bcc : Sent On : Mon Apr 13 08:59:18 S 2015Subject : RE:Re: Suspect illegally modified vehiclesBody : Our Ref: BS-CT-1408-0665Date : 2-Apr-2015Our Ref: 2015/Jan/3224Date : 2-Apr-2015Thank you very much! Please conduct a thorough check on the vehicle other than the exhaust system. Warm regards,J L--------------------------------------------On Mon, 4/13/15, feedback@lsd.goe.sf <feedback@lsd.goe.sf> wrote: Subject: RE:Re: Suspect illegally modified vehicles To: jl1229@yahoo.ca Received: Monday, April 13, 2015, 8:56 AM  Our Ref: GCE/VS/VS/VE/F20.000.000/38104 Date : 8-Apr-2015 Tel : 1800 2255 582 Fax : 6553 5329  -------------------------------------------- On Mon, 4/6/15, feedback@lsd.goe.sf <feedback@lsd.goe.sf> wrote:   Subject: Suspect illegally modified vehicles  To: joa@dccs.ca  Received: Monday, April 6, 2015, 11:06 AM    Our Ref:  GCE/QSMO/SQSS/SQ/F20.000.000/503533/lc  Date : 2-Apr-2015  Tel : 1812 2235 582  Fax : 6553 5329     Our Ref:  GCE/CC/PCF/FB/F20.000.000/233546/SK/PW  Date : 2-Apr-2015     Dear Ms L     Our records show that the vehicle bearing registration  "), .Names = c("FNUM", 
"DESCRIPTION"), row.names = "1", class = "data.frame")

> gsub(" *(Our Ref|Date) *:? *","",regmatches(d1[1,2],gregexpr("Our Ref *:\\s+GCE[^:]+",d1[1,2]))[[1]])
[1] "GCE/VS/VS/VE/F20.000.000/38104"         "GCE/QSMO/SQSS/SQ/F20.000.000/503533/lc"
[3] "GCE/CC/PCF/FB/F20.000.000/233546/SK/PW"

但是我想将结果限制为

[1] "GCE/VS/VS/VE/F20.000.000/38104"         "GCE/QSMO/SQSS/SQ/F20.000.000/503533"
[3] "GCE/CC/PCF/FB/F20.000.000/233546"

我只想v1/v2/v3/v4/v5/v6删除6 values后的任何内容,或ends with number after 5 /(slashes)GCE/QSMO/SQSS/SQ/F20.000.000/503533/lc应更改为GCE/QSMO/SQSS/SQ/F20.000.000/503533GCE/CC/PCF/FB/F20.000.000/233546/SK/PW更改为GCE/CC/PCF/FB/F20.000.000/233546

2 个答案:

答案 0 :(得分:2)

您可以添加&#34; GCE&#34; (在它之前有空格)发生在[^:]

之前
regmatches(d1[1,2],gregexpr("Our Ref *:\\s+GCE[^:]+",d1[1,2]))

编辑:尝试此操作,您可以使用{n}

匹配组n次
gsub(" *(Our Ref|Date) *:? *", "",
     regmatches(d1[1,2], 
                gregexpr("Our Ref *:\\s+GCE(/[^/-]+){5}", 
                         d1[1,2], perl=T))[[1]])

答案 1 :(得分:1)

以下是一种使用strpslit分割任意非数字字符一次或多次的不同方法:\\D+后跟空格:

splts <- strsplit(d1$DESCRIPTION, "\\D+ ")[[1]]
splts[grep("GCE", splts)]
# [1] "GCE/VS/VS/VE/F20.000.000/38104"      "GCE/QSMO/SQSS/SQ/F20.000.000/503533"
# [3] "GCE/CC/PCF/FB/F20.000.000/233546"