1

Say I have a list of strings L , L2= "some_text-to-delete" and I want to delete all the Li that match with "text" or "delete" DeleteCase only gives the ones that match exactly...

kglr
  • 394,356
  • 18
  • 477
  • 896
Kafkarudo
  • 619
  • 5
  • 9

1 Answers1

1

Here are some suggestions:

let list={"element1","element2","text","text","element5"}

DeleteCases[list, x_ /; StringMatchQ[x, "*text*", IgnoreCase -> True]]    

Select[list, StringFreeQ[#, "text", IgnoreCase -> True] &]    

Extract[list,Position[list,x_String/;StringFreeQ[x,"text",IgnoreCase->True]]]

{"element1", "element2", "element3"}

For a timing test, if we do list2 = Flatten@Table[list, {100000}];

(t1=DeleteCases[list2,x_/;StringMatchQ[x,"*text*",IgnoreCase->True]])//AbsoluteTiming//First
(t2=Select[list2,StringFreeQ[#,"text",IgnoreCase->True]&])//AbsoluteTiming//First
(t3=Extract[list2,Position[list2,x_String/;StringFreeQ[x,"text",IgnoreCase->True]]])//AbsoluteTiming//First
t1==t2==t3

True

0.624

0.882

1.110

So t1 is the best.

Murta
  • 26,275
  • 6
  • 76
  • 166