【问题标题】:Parse long string to retrieve channel_id解析长字符串以检索 channel_id
【发布时间】:2021-11-25 21:11:53
【问题描述】:

我从 Telegram 中提取了很多数据。但是,我无法隔离 channel_id。现在我有一个长字符串,在许多其他信息中包含channel_id。问题是如何删除除 channel_id 之外的所有内容,即 "channel_id=XXXXXXXXXX) 之后的数字?

我的 data.frame 的子集

df <- structure(list(channel_id = c("MessageFwdHeader(date=datetime.datetime(2021, 5, 13, 20, 50, 47, tzinfo=datetime.timezone.utc), imported=False, from_id=PeerChannel(channel_id=1292436059), from_name=None, channel_post=1404, post_author=None, saved_from_peer=None, saved_from_msg_id=None, psa_type=None)", 
                                      "MessageFwdHeader(date=datetime.datetime(2021, 5, 4, 9, 24, 16, tzinfo=datetime.timezone.utc), imported=False, from_id=PeerChannel(channel_id=1480423705), from_name=None, channel_post=224, post_author=None, saved_from_peer=None, saved_from_msg_id=None, psa_type=None)", 
                                      "MessageFwdHeader(date=datetime.datetime(2021, 3, 25, 14, 9, 38, tzinfo=datetime.timezone.utc), imported=False, from_id=PeerChannel(channel_id=1489900933), from_name=None, channel_post=627, post_author=None, saved_from_peer=None, saved_from_msg_id=None, psa_type=None)", 
                                      "MessageFwdHeader(date=datetime.datetime(2021, 3, 12, 22, 10, 3, tzinfo=datetime.timezone.utc), imported=False, from_id=PeerChannel(channel_id=1455689590), from_name=None, channel_post=1457, post_author=None, saved_from_peer=None, saved_from_msg_id=None, psa_type=None)", 
                                      "MessageFwdHeader(date=datetime.datetime(2021, 3, 9, 12, 52, 5, tzinfo=datetime.timezone.utc), imported=False, from_id=PeerChannel(channel_id=1348575245), from_name=None, channel_post=None, post_author=None, saved_from_peer=None, saved_from_msg_id=None, psa_type=None)"
)), row.names = c(NA, -5L), class = c("data.table", "data.frame"))

想要的结果

channel_id <- structure(list(channel_id = c("1292436059", 
                                            "1480423705", 
                                            "1489900933", 
                                            "1455689590", 
                                            "1348575245"
)), row.names = c(NA, -5L), class = c("data.table", "data.frame"))

【问题讨论】:

  • 你可能只能在channel_id= 之后找到),但如果它们的长度都相同,你可以进行两次传递,.split() 字面上是"channel_id=",然后检查您返回的内容是否符合更简单的正则表达式

标签: r string parsing


【解决方案1】:

您可以尝试regexpr,使用(?&lt;=\\(channel_id=) 查找(channel_id=,而不是匹配数字\\d+,并使用(?=\\)) 查找),然后使用regmatches 提取匹配项.

regmatches(df$channel_id, regexpr("(?<=\\(channel_id=)\\d+(?=\\))"
          , df$channel_id, perl=TRUE))
#[1] "1292436059" "1480423705" "1489900933" "1455689590" "1348575245"

或组合两个sub。

sub(").*", "", sub(".*\\(channel_id=", "", df$channel_id))
#[1] "1292436059" "1480423705" "1489900933" "1455689590" "1348575245

【讨论】:

  • 太棒了!谢谢@GKi
【解决方案2】:

我们可能会使用str_extract

library(stringr)
library(dplyr)
df %>%
    transmute(channel_id = str_extract(channel_id, "(?<=channel_id\\=)\\d+"))
   channel_id
1: 1292436059
2: 1480423705
3: 1489900933
4: 1455689590
5: 1348575245

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 2015-06-01
    • 2011-01-30
    • 1970-01-01
    • 2018-03-08
    • 2012-07-03
    • 2021-12-26
    相关资源
    最近更新 更多