简体   繁体   English

正则表达式 R 提取多个字符串匹配

[英]Regex R Extracting multiple string matches

In R, I am looking to parse a succession of log data into categorised events.在 R 中,我希望将一系列日志数据解析为分类事件。

I have a vector call regex_text, it's a continuous string (line breaks added for clarity here):我有一个向量调用 regex_text,它是一个连续的字符串(为了清楚起见,这里添加了换行符):

21/08/2014 22:58CONTENT_ACCESS.preparing
21/08/2014 23:00EXE_IN.preparing
21/08/2014 23:07CONTENT_ACCESS.preparing
21/08/2014 23:08CONTENT_ACCESS.preparing
21/08/2014 23:12EXE_CO.preparing
21/08/2014 23:28EXE_IN.preparing
21/08/2014 23:29CONTENT_ACCESS.preparing
21/08/2014 23:30CONTENT_ACCESS.preparing

and would like to use regex to process the first and last timestamps of each sequences of 'CONTENT_ACCESS.preparing' and place them into this dataframe:并想使用正则表达式来处理“CONTENT_ACCESS.preparing”的每个序列的第一个和最后一个时间戳并将它们放入此数据帧中:

          start_ts          stop_ts
1 21/08/2014 22:58 21/08/2014 22:58
          start_ts          stop_ts
2 21/08/2014 23:07 21/08/2014 23:08
          start_ts          stop_ts
3 21/08/2014 23:29 21/08/2014 23:30

There could be many repetitions of 'CONTENT_ACCESS.preparing', my example just has two instances with two entries in each. 'CONTENT_ACCESS.preparing' 可能有很多重复,我的例子只有两个实例,每个实例有两个条目。

The code below can be run directly in R and currently outputs a single entry:下面的代码可以直接在 R 中运行,目前输出一个条目:

          start_ts          stop_ts
1 21/08/2014 23:07 21/08/2014 23:30

I would like guidance on how to extract two entries, as above我想获得有关如何提取两个条目的指导,如上所述

Code:代码:

library(stringr)
options(stringsAsFactors = FALSE)

eventised_session <- data.frame(start_ts=as.character(),
                                stop_ts=as.character())

regex_text <- "21/08/2014 22:58CONTENT_ACCESS.preparing21/08/2014 23:00EXE_IN.preparing21/08/2014 23:07CONTENT_ACCESS.preparing21/08/2014 23:08CONTENT_ACCESS.preparing21/08/2014 23:12EXE_CO.preparing21/08/2014 23:28EXE_IN.preparing21/08/2014 23:29CONTENT_ACCESS.preparing21/08/2014 23:30CONTENT_ACCESS.preparing"
regex_pattern <- "(\\d{2}\\/\\d{2}\\/\\d{4}\\s\\d{2}\\:\\d{2})(CONTENT_ACCESS\\.preparing)"


if (grepl(regex_pattern, regex_text, perl=TRUE)) {

  sm <- str_match_all(regex_text, regex_pattern )

  #Get the first and last timestamp in matched sequence
  r_start_ts <- sapply(sm, function(x) x[1, 2])
  r_stop_ts <- sapply(sm, function(x) x[sapply(sm,nrow), sapply(sm, ncol) - 1])

  eventised_session[nrow(eventised_session)+1,] <- c(r_start_ts, r_stop_ts)
  print(eventised_session)
}  

Are you looking for something like this?你在寻找这样的东西吗?

vec = c("21/08/2014 23:07CONTENT_ACCESS.preparing", "21/08/2014 23:08CONTENT_ACCESS.preparing", "21/08/2014 23:12EXE_CO.preparing", "21/08/2014 23:28EXE_IN.preparing", "21/08/2014 23:29CONTENT_ACCESS.preparing", "21/08/2014 23:30CONTENT_ACCESS.preparing")

setNames(data.frame(gsub("[a-z].*","",t(matrix(grep("CONTENT_ACCESS.preparing",vec,value=T),2)),T)),c("Start_ts","Stop_ts"))
         Start_ts          Stop_ts
1 21/08/2014 23:07 21/08/2014 23:08
2 21/08/2014 23:29 21/08/2014 23:30

If you have the regex_text as in your code then you could do:如果您的代码中有regex_text ,那么您可以执行以下操作:

regex_text <- "21/08/2014 23:07CONTENT_ACCESS.preparing21/08/2014 23:08CONTENT_ACCESS.preparing21/08/2014 23:12EXE_CO.preparing21/08/2014 23:28EXE_IN.preparing21/08/2014 23:29CONTENT_ACCESS.preparing21/08/2014 23:30CONTENT_ACCESS.preparing"

a = gsub(".*?(\\d{2}/\\d{2}/\\d{4} \\d{2}:\\d{2})CONTENT_ACCESS[.]preparing.*?","\\1\n",regex_text)

setNames(data.frame(matrix( unlist(strsplit(a,"\n")),ncol = 2,byrow = T)),c("start_ts","stop_ts"))
          start_ts          stop_ts
1 21/08/2014 23:07 21/08/2014 23:08
2 21/08/2014 23:29 21/08/2014 23:30

FOR THE NEW EDIT:对于新编辑:

 a = gsub("((.*?preparing){2})","\\1\n ",regex_text)
 b = read.table(text=gsub("(?<=preparing)(?=\\d+)","|",a,perl=T),sep="|",fill=T,h=F)
 d = sub("^(?:(?!CONTENT).)*$|(^.*)CONTENT.*$","\\1",as.matrix(b),perl=T)
 subset(data.frame(start_ts = d[,1],stop_ts = ifelse(d[,2]=="",d[,1],d[,2])),start_ts!="")
           start_ts          stop_ts
1  21/08/2014 22:58 21/08/2014 22:58
2  21/08/2014 23:07 21/08/2014 23:08
4  21/08/2014 23:29 21/08/2014 23:30

you can create a list of matching instances you are looking for and create a loop您可以创建您正在寻找的匹配实例列表并创建一个循环

for (match in match_list){
#match as in your code
#add into db as in your code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM