3
$\begingroup$

I have a list:

lis = {{"abc", 1, 2}, {"cde", 3, 4}, {"fgbc", 5, 6}}

I would like to select list items that have first elements that end in the string "bc" to give:

res = {{1,2},{5,6}}

This is a variant on this question which has to do with strings only. Thank you as always for suggestions.

$\endgroup$
5
$\begingroup$
Pick[lis[[All, 2 ;;]], StringEndsQ[lis[[All, 1]], "bc"]]

{{1, 2}, {5, 6}}

Also

f[{_String?(StringEndsQ["bc"]), x__}] := {x}
f[_] := Sequence[]
f /@ lis

{{1, 2}, {5, 6}}

$\endgroup$
4
$\begingroup$
lis = {{"abc", 1, 2}, {"cde", 3, 4}, {"fgbc", 5, 6}};

Using Cases

Cases[lis, {_?(StringTake[#, -2] == "bc" &), x_, y_} :> {x, y}, 
  Infinity]

(* {{1, 2}, {5, 6}} *)

or

Rest /@ Cases[lis, {_?(StringTake[#, -2] == "bc" &), __}, Infinity]

(* {{1, 2}, {5, 6}} *)

Or using Select

Rest /@ Select[lis, StringTake[#[[1]], -2] == "bc" &]

(* {{1, 2}, {5, 6}} *)

Or using DeleteCases

Rest /@ DeleteCases[lis, {_?(StringTake[#, -2] != "bc" &), __}, 
  Infinity]

(* {{1, 2}, {5, 6}} *)
$\endgroup$
3
$\begingroup$

This uses string matching on the first element of each sublist and then discards the first element from the matches.

lis = {{"abc", 1, 2}, {"cde", 3, 4}, {"fgbc", 5, 6}};
Rest/@Select[lis,StringMatchQ[First[#],RegularExpression["\\w*bc"]]&]

(* {{1, 2}, {5, 6}} *)

You may need to adjust the pattern given to RegularExpression if your strings do not all begin with word characters.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.