Find all strings which has sub-string of an item in another string list

StackOverflow https://stackoverflow.com/questions/18990661

  •  29-06-2022
  •  | 
  •  

Вопрос

I have the following code.

let s1 = [(12, "abcde12345"); (23, "bcdef2345"); (12, "xyzafg3838")]
let s2 = ["bcd"; "345"]

What's the best way to find all items in s1 which second item has sub-string of any one in s2?

(12, "abcde12345"); (23, "bcdef2345")

In my real code s1 is a Seq.

Это было полезно?

Решение 2

I figured out one.

s1 |> Seq.filter (fun i -> List.exists (fun e -> (snd i).Contains(e)) s2)

Другие советы

Seq.filter (fun (_, x) -> List.exists (x.Contains) s2) s1

Concat all of the items from the second set into a regular expression, then apply it on each item in the first set.

open System
open System.Text.RegularExpressions

let setA  = [ "One"; "Two"; "Three" ]
let setB = [ "o"; "n" ];

let pattern = String.Join("|", setB);
let regex = new Regex(pattern);

let results = setA |> List.filter (fun str -> regex.Match(str).Success)

results |> List.iter (fun result -> Console.WriteLine(result))
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top