How to extract from a list based on a partial alphanumeric string?

Consider the following list. I've tried various ways to extract only those pairs in the list where both elements have the string N36. Given the below, the output would be just {N36012,N36321} and {N36040,N36098}. Suggestions are most welcomed!

lst = {{N36061, N35100}, {N36012, N36321}, {N36040, N36098}, {N8100,
N36010}}


Clear["Global*"]

lst = {{N36061, N35100}, {N36012, N36321}, {N36040, N36098}, {N8100, N36010}};

Select[lst,
And @@ {StringContainsQ[ToString@#[[1]], "N36"],
StringContainsQ[ToString@#[[2]], "N36"]} &]

(* {{N36012, N36321}, {N36040, N36098}} *)


Or

Select[lst, AllTrue[#, StringContainsQ[ToString@#, "N36"] &] &]

(* {{N36012, N36321}, {N36040, N36098}} *)

• Many thanks, Bob ... I wasn't aware of the StringContainsQ ... thank u ...prg Aug 5, 2022 at 14:01
• With operator forms: Select[lst, AllTrue[StringStartsQ["N36"]]] Oct 6, 2023 at 14:24

There is a built in StringStartsQ. You can define a function to determine whether items in a list share a prefix:

PrefixMatch[prefix_String, list_List] :=
AllTrue[ToString /@ list, StringStartsQ[prefix]]


With that, you can use Select:

Select[lst, PrefixMatch["N36", #] &]

• PrefixMatch!! very nice ... thank u!! Aug 5, 2022 at 14:02

have the string N36.

It you meant starts with "N36" and not has this string anywhere, another option is

lst = {{"N36061", "N35100"}, {"N36012", "N36321"}, {"N36040", "N36098"}, {"N8100", "N36010"}}
pat = "N36" ~~ __;
StringMatchQ[First[#], pat] && StringMatchQ[Last[#], pat] & /@ lst;
Pick[lst, %]


• Nasser: That is a very nice option too; I was not familiar with your second line of code ... thank u very much! Aug 4, 2022 at 17:29
Clear["Global*"]
lst = {{"N36061", "N35100"}, {"N36012", "N36321"}, {"N36040",
"N36098"}, {"N8100", "N36010"}}

Pick[lst, And @@@ Map[StringContainsQ["N36"], lst, {2}]]

Pick[lst, AllTrue[#, StringContainsQ["N36"]] & /@ lst]

DeleteCases[lst, {OrderlessPatternSequence[_?(StringFreeQ[
"N36"]), _]}]


Result

{{"N36012", "N36321"}, {"N36040", "N36098"}}

lst = {{"N36061", "N35100"}, {"N36012", "N36321"}, {"N36040", "N36098"}, {"N8100", "N36010"}}

sel[x_] /; And @@ Map[StringStartsQ["N36"], x] := x
sel[_] := Nothing

sel /@ lst


{{"N36012", "N36321"}, {"N36040", "N36098"}}

checkPre = ToString /* StringMatchQ[RegularExpression["^N36.*"]];

{{N36061, N35100}, {N36012, N36321}, {N36040, N36098}, {N8100, N36010}} //
Cases[{_?checkPre, _?checkPre}]