Home > Net >  Find strings where the first half matches the second
Find strings where the first half matches the second

Time:01-13

I have a list of IP address pairs separated by "::".

ip_pairs <- c("104.124.199.136::192.168.1.67", "104.124.199.136::192.168.137.174", "192.168.1.67::104.124.199.136", "192.168.137.174::104.124.199.136")

As you can see, the third and fourth elements of the vector are the same as the first two, but reversed (my actual problem is to find all unique pairings of IPs, so the solution would drop the pair B::A if A::B is already present. This could be solved using stringr or regex, I'm guessing.

CodePudding user response:

Use read.table to create a two column data frame from the pairs, sort each row and find the duplicates using duplicated. Then extract out the non-duplicates. No packages are used.

DF <- read.table(text = ip_pairs, sep = ":")[-2]
ip_pairs[! duplicated(t(apply(DF, 1, sort)))]
## [1] "192.168.1.67::104.124.199.136" "192.168.137.174::104.124.199.136"

CodePudding user response:

One option:

library(stringr)
split_function = function(x) {
    x = sort(x)
    paste(x, collapse="::")
}

pairs = str_split(ip_pairs, "::")

unique(sapply(pairs, split_function))
[1] "104.124.199.136::192.168.1.67"    "104.124.199.136::192.168.137.174"
  •  Tags:  
  • Related