Function Repository Resource:

StringWithinQ

Source Notebook

Test if a string pattern is within an expression

Contributed by: Michael Sollami

ResourceFunction["StringWithinQ"][expr,patt]

returns True if expr contains a string matching pattern patt.

ResourceFunction["StringWithinQ"][patt]

represents an operator form of ResourceFunction["StringWithinQ"] that can be applied to an expression.

Details and Options

ResourceFunction["StringWithinQ"] works on arbitrary expressions, unlike StringContainsQ, which only works on strings or lists of strings.
ResourceFunction["StringWithinQ"] takes the following options:
IgnoreCaseFalsewhether to ignore the casing
"ExactMatch"Falsewhether to enforce exact matches (not substring matches)
ResourceFunction["StringWithinQ"] works on associations by testing values but not keys.

Examples

Basic Examples (3) 

Check if a substring is anywhere within an expression:

In[1]:=
ResourceFunction["StringWithinQ"][{"happy" -> 1, "sad" -> 0}, "app"]
Out[1]=

Use any valid string pattern for matching:

In[2]:=
assn = <|"pi" -> "3.141", "phi" -> "1.619"|>;
ResourceFunction[
 "StringWithinQ"][<|"pi" -> "3.141", "phi" -> "1.619"|>, DigitCharacter ~~ "." ~~ DigitCharacter .., "ExactMatch" -> True]
Out[3]=

Use the operator form of StringWithinQ:

In[4]:=
ResourceFunction["StringWithinQ"]["da"] /@ <|-1 -> "yesterday", 0 -> "today", 1 -> "tomorrow"|>
Out[4]=

Options (2) 

By default, the casing of the pattern is used in matching. You can override this with the IgnoreCase option:

In[5]:=
expr = {1, {"Hello", 1, {"Hi"}}, 1};
ResourceFunction["StringWithinQ"][expr, "h"]
Out[6]=
In[7]:=
ResourceFunction["StringWithinQ"][expr, "h", IgnoreCase -> True]
Out[7]=

By default, StringWithinQ looks for substring matches:

In[8]:=
ResourceFunction["StringWithinQ"][{"a", "ab", "abc"}, "bc"]
Out[8]=

You can override this to enforce that patterns match exact strings with the "ExactMatch" option:

In[9]:=
ResourceFunction["StringWithinQ"][{"a", "ab", "abc"}, "bc", "ExactMatch" -> True]
Out[9]=
In[10]:=
ResourceFunction["StringWithinQ"][{"a", "ab", "abc"}, _ ~~ "bc", "ExactMatch" -> True]
Out[10]=

Properties and Relations (2) 

StringWithinQ is like StringContainsQ, except that it looks deeper inside expressions for a matching string:

In[11]:=
ResourceFunction["StringWithinQ"][{{"a", "b"}, {"c", "d"}}, "a"]
Out[11]=
In[12]:=
Quiet@StringContainsQ[{{"a", "b"}, {"c", "d"}}, "a"]
Out[12]=

StringFreeQ does not take level specs like FreeQ does, so StringWithinQ is useful here:

In[13]:=
! FreeQ[{"happy" -> 1, "sad" -> 0}, (__ ~~ "app" ~~ __)]
Out[13]=
In[14]:=
ResourceFunction[
 "StringWithinQ"][{"happy" -> 1, "sad" -> 0}, (__ ~~ "app" ~~ __)]
Out[14]=

Possible Issues (1) 

Like Cases, keys in associations are not matched:

In[15]:=
Cases[<|"a" -> 1|>, "a", \[Infinity]]
Out[15]=
In[16]:=
ResourceFunction["StringWithinQ"][<|"a" -> 1|>, "a"]
Out[16]=

Publisher

Michael Sollami

Version History

  • 1.0.0 – 28 February 2020

Related Resources

License Information