2
$\begingroup$

Here is what I want to do:

I have a list of strings for example

s={"red carpet","red box","black carpet","black box"}

I want to use something like

Select[s, StringMatchQ[#, "red"] &] (*to get an output*)
{"red carpet","red box"}

thx in advance for your help

$\endgroup$
5
  • $\begingroup$ Try this Select[s, StringMatchQ[#, ___ ~~ "red" ~~ ___] &]!! $\endgroup$ Jan 28, 2015 at 13:04
  • $\begingroup$ Have you seen StringMatchQ documentation page? $\endgroup$
    – Kuba
    Jan 28, 2015 at 13:04
  • $\begingroup$ I got it now, Thank you guys $\endgroup$
    – kevin
    Jan 28, 2015 at 13:08
  • $\begingroup$ Or this Select[s, StringPosition[#, "red"] != {} &] $\endgroup$
    – tchronis
    Jan 28, 2015 at 13:22
  • $\begingroup$ This is using the solution I have you in a comment on your last question. When you receive a solution, it's a good idea to try to understand how it works. Start by looking up the functions in the documentation. Reading up on StringMatchQ would have immediately solved this problem. $\endgroup$
    – Szabolcs
    Jan 28, 2015 at 19:01

1 Answer 1

1
$\begingroup$

You were looking for StringFreeQ I think:

Select[s, ! StringFreeQ[#, "red"] &]

Another way, StringCases with a regular expression:

StringCases[s, RegularExpression[".*red.*"]] // Flatten
$\endgroup$
1
  • $\begingroup$ Everyone I know has there own StringContains[] method $\endgroup$
    – M.R.
    Jan 28, 2015 at 16:07

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