skip to Main Content

I have extracted a lot of data from Telegram. However, I was not able to isolate the channel_id. Now I have a long string that among a lot of other information contain channel_id. Question is how do I remove everything apart from the channel_id i.e. the numbers following "channel_id=XXXXXXXXXX)?

Subset of my 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"))

Desired result

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

2

Answers


  1. You can try regexpr with a look behind for (channel_id= using (?<=\(channel_id=), than match digit(s) \d+ and look ahead for ) using (?=\)) and extract the matches using regmatches.

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

    or combining two sub.

    sub(").*", "", sub(".*\(channel_id=", "", df$channel_id))
    #[1] "1292436059" "1480423705" "1489900933" "1455689590" "1348575245
    
    Login or Signup to reply.
  2. We may use 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search