gpt4 book ai didi

regex - R正则表达式提取文本不紧跟模式

转载 作者:行者123 更新时间:2023-12-02 08:43:59 26 4
gpt4 key购买 nike

我有一个数据框,其中有一列包含半结构化文本。我正在寻找某些信息,但它们具有我不确定如何利用的模式。

head(events5)
flight_history_id date_time_recorded event
862 280604281 2012-11-13 17:55:58.673-08 Arrival Estimation
5514 280316415 2012-11-12 17:25:08.814-08 Time Adjustment
5527 280315758 2012-11-12 16:10:06.968-08 Time Adjustment
5539 280316495 2012-11-12 15:38:10.099-08 Time Adjustment
5545 280303866 2012-11-13 06:51:45.831-08 Time Adjustment
5558 280303866 2012-11-12 05:50:23.547-08 Time Adjustment
data_updated
862 EGA- Based on Distance and Airspeed
5514 EGD- New=11/13/12 20:20, DGATE- New=A1, EGA- New=11/13/12 21:00
5527 EGD- New=11/13/12 19:05, DGATE- New=A7, EGA- New=11/13/12 20:50, AGATE- New=C5
5539 EGD- New=11/13/12 06:35, DGATE- New=A1, EGA- New=11/13/12 07:15
5545 AGD- Old=11/13/12 07:40 New=11/13/12 07:38, EGA- Old=11/13/12 10:25 New=11/13/12 10:18
5558 EGD- New=11/13/12 07:45, DGATE- New=11, EGA- New=11/13/12 10:25, AGATE- New=A1

这是输出:

structure(list(flight_history_id = c(280604281L, 280316415L, 
280315758L, 280316495L, 280303866L, 280303866L, 280271465L, 280271465L,
280271465L, 280271465L), date_time_recorded = structure(c(10L,
4L, 3L, 2L, 7L, 1L, 9L, 8L, 6L, 5L), .Label = c("2012-11-12 05:50:23.547-08",
"2012-11-12 15:38:10.099-08", "2012-11-12 16:10:06.968-08", "2012-11-12 17:25:08.814-08",
"2012-11-12 17:58:14.268-08", "2012-11-13 05:16:41.01-08", "2012-11-13 06:51:45.831-08",
"2012-11-13 07:38:19.593-08", "2012-11-13 07:54:22.588-08", "2012-11-13 17:55:58.673-08"
), class = "factor"), event = structure(c(1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L), .Label = c("Arrival Estimation", "Time Adjustment"
), class = "factor"), data_updated = structure(c(3L, 8L, 7L,
4L, 2L, 5L, 1L, 10L, 9L, 6L), .Label = c("AGD- New=11/13/12 10:22, EGA- Old=11/13/12 11:20 New=11/13/12 11:09",
"AGD- Old=11/13/12 07:40 New=11/13/12 07:38, EGA- Old=11/13/12 10:25 New=11/13/12 10:18",
"EGA- Based on Distance and Airspeed", "EGD- New=11/13/12 06:35, DGATE- New=A1, EGA- New=11/13/12 07:15",
"EGD- New=11/13/12 07:45, DGATE- New=11, EGA- New=11/13/12 10:25, AGATE- New=A1",
"EGD- New=11/13/12 08:55, EGA- New=11/13/12 09:45, AGATE- New=A1",
"EGD- New=11/13/12 19:05, DGATE- New=A7, EGA- New=11/13/12 20:50, AGATE- New=C5",
"EGD- New=11/13/12 20:20, DGATE- New=A1, EGA- New=11/13/12 21:00",
"EGD- Old=11/13/12 08:55 New=11/13/12 10:05, EGA- Old=11/13/12 09:45 New=11/13/12 10:55",
"EGD- Old=11/13/12 10:05 New=11/13/12 10:30, EGA- Old=11/13/12 10:55 New=11/13/12 11:20"
), class = "factor")), .Names = c("flight_history_id", "date_time_recorded",
"event", "data_updated"), row.names = c(862L, 5514L, 5527L, 5539L,
5545L, 5558L, 5564L, 5566L, 5570L, 5572L), class = "data.frame")

我正在尝试从 'data_updated' 列中提取以 'EGA' 开头的部分的字段 'New=' 部分>。问题是有时两者之间有一个“Old=”字段。我查看了使用 stringr::str_split 并将列转换为列表,但仍然有同样的问题。我认为这是一个正则表达式/gsub 问题,但不知 Prop 体如何实现。

有什么建议吗?

编辑:我希望最终产品是具有 EGA/新值的向量/列。所以像这样:

NA
11/13/12 21:00
11/13/12 20:50
11/13/12 07:15
11/13/12 10:18
11/13/12 10:25

最佳答案

与@Pharaoh 的正则表达式相比,这是一个替代方案,它在 EGA-tag

之后使用无限量的文本
/EGA-(?:(?!New=|").)+(New=[:\d\/ ]+)/g

如果否定前瞻中的 " 过于笼统,您可以将其更改为 EGD

演示+说明: http://regex101.com/r/qY4uA0

如果不需要,请删除标志和分隔符。

关于regex - R正则表达式提取文本不紧跟模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14034740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com