Regex 清洁';stringr str_替换所有';多次匹配时自动连接
我使用了Regex 清洁';stringr str_替换所有';多次匹配时自动连接,regex,r,string,substring,stringr,Regex,R,String,Substring,Stringr,我使用了police\u officer您可以将以下正则表达式与str\u match\u all一起使用: \bID:\s*(\w+(?:\h+\w+)*) 见 谢谢我想真正的问题是当我把所有的东西都放在一个数据框中,其中包括接线员,时间,地址,以及警官时间我不知道您的最终数据帧应该是什么样子,但请注意,您刚刚添加了str\u match\u all的整个输出,而您只需要[,2]维度。尝试BPD_log刚刚看到您的更新,但我希望数据显示在一行下,这意味着所有警察都应该在一个单元格中。如果你能
police\u officer您可以将以下正则表达式与str\u match\u all
一起使用:
\bID:\s*(\w+(?:\h+\w+)*)
见
谢谢我想真正的问题是当我把所有的东西都放在一个数据框中,其中包括接线员
,时间
,地址
,以及警官
<代码>时间我不知道您的最终数据帧应该是什么样子,但请注意,您刚刚添加了str\u match\u all
的整个输出,而您只需要[,2]
维度。尝试BPD_log刚刚看到您的更新,但我希望数据显示在一行下,这意味着所有警察都应该在一个单元格中。如果你能做到这一点,那就太好了。BPD\u log呢?我不知道你需要什么?
> txt <- "Call Taker: Telephone Operators Sharon L Moran\n Location/Address: [BRO 6949] 61 WILSON ST\n ID: Patrolman Darvin Anderson\n Disp-22:43:39 Arvd-22:48:57 Clrd-23:49:45\n ID: Patrolman Stephen T Pina\n Disp-22:43:48 Clrd-22:46:10\n ID: Sergeant Michael V Damiano\n Disp-22:46:33 Arvd-22:47:14 Clrd-22:55:22"
> str_match_all(txt, "\\bID:\\s*(\\w+(?:\\h+\\w+)*)")
[[1]]
[,1] [,2]
[1,] "ID: Patrolman Darvin Anderson" "Patrolman Darvin Anderson"
[2,] "ID: Patrolman Stephen T Pina" "Patrolman Stephen T Pina"
[3,] "ID: Sergeant Michael V Damiano" "Sergeant Michael V Damiano"
> time <- str_trim(str_extract(txt, " [[:digit:]]{4}"))
> Call_taker <- str_replace_all(str_extract(txt, "Call Taker:.*\n"),"Call Taker:","" ) %>% str_replace_all("\n","")
> address <- str_extract(txt, "Location/Address:.*\n")
> Police_officer <- str_match_all(txt, "\\bID:\\s*(\\w+(?:\\h+\\w+)*)")
> BPD_log <- cbind(time,Call_taker,address,list(Police_officer[[1]][,2]))
> BPD_log <- as.data.frame(BPD_log)
> colnames(BPD_log) <- c("time", "Call_taker", "address", "Police_officer")
> BPD_log
time Call_taker address
1 6949 Telephone Operators Sharon L Moran Location/Address: [BRO 6949] 61 WILSON ST\n
Police_officer
1 Patrolman Darvin Anderson, Patrolman Stephen T Pina, Sergeant Michael V Damiano
>