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...
Asked
Active
Viewed 532 times
1
1 Answers
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
-
1Thank you for that answer, it really help me,you do know how to teach :) – Kafkarudo Jul 20 '14 at 22:13
StringMatchQand string patterns:DeleteCases[yourlist,z_/;StringMatchQ[z,___~~"text"~~___,IgnoreCase->True]etc – Mike Honeychurch Jul 14 '14 at 20:40