Extract elements from a list using RegularExpression

I want to extract the “DownValues”, “OwnValues”, “SubValues”, and “UpValues” from the following result:

In[38]:= "System`" /. (Information["*Values"] /. _[{a_}] :> a)

Out[38]= {"DefaultValues", "DownValues", "DynamicModuleValues", \
"FormatValues", "NSolveValues", "NValues", "OwnValues", \
"SingularValues", "SolveValues", "SubValues", \
"SynthesizeMissingValues", "UpValues", "Values"}

Is there any trick to achieve this goal?

Regards,
HZ

Not sure what you mean. If you already have the list of symbol names you are interested in why do you need to extract them from another list? Anyway, one way to do it using Names rather than pattern matching.

Names["System`*Values"] // 
  Select[MemberQ[{"DownValues", "OwnValues", "SubValues", "UpValues"}, #] &] //
  Map[Information]

Thank you for your comment. Let me add a few words in response:

  1. As for regex, this is just a minimal example I want to show. In fact, I want to some tips with the pattern like “(Down|Own|Sub|Up)Values”.

  2. The following results are equivalent:

    In[29]:= Map[Information][List[DownValues]] ==
    Map[Information, List[DownValues]]

    Out[29]= True

So, this implies that the following commands are equivalent:

Map[Information][List[DownValues]]
Map[Information, List[DownValues]]

But I can’t find the document specification for the first usage mentioned above.

I want to some tips with the pattern like “(Down|Own|Sub|Up)Values”.

That part was not clear in your original question.

Using StringExpression

Names["System`*Values"] // 
 Select[StringMatchQ[StartOfString ~~ "Down" | "Own" | "Sub" | "Up" ~~ "Values"]]

Using RegularExpression

Names["System`*Values"] // 
 Select[StringMatchQ[RegularExpression["^(Down|Own|Sub|Up)Values"]]]

This is the operator form of Map and is documented

Map[Information][List[DownValues]]

Map[f] represents an operator form of Map that can be applied to an
expression.

Many built-in functions support an operator form. Take a look at this

The Select and StringMatchQ example above uses the operator form.

Another example - which primes in the range 1 - 1000 have a digit sum that is also prime.

Range[1000] // Select[PrimeQ] // AssociationMap[IntegerDigits/*Total] // Select[PrimeQ]

Thank you for your explanation. I still have another question as describe below.

The following form works:

In[28]:= Select[Names["System`*Values"], 
 StringMatchQ[
  StartOfString ~~ "Down" | "Own" | "Sub" | "Up" ~~ "Values"], 2]

Out[28]= {"DownValues", "OwnValues"}

But the following fails:

In[11]:= Names["System`*Values"] // 
 Select[StringMatchQ[
   StartOfString ~~ "Down" | "Own" | "Sub" | "Up" ~~ "Values"], 2]

During evaluation of In[11]:= StringMatchQ::argt: StringMatchQ called with 0 arguments; 1 or 2 arguments are expected.

Out[11]= StringMatchQ[][{"DefaultValues", "DownValues", 
  "DynamicModuleValues", "FormatValues", "NSolveValues", "NValues", 
  "OwnValues", "SingularValues", "SolveValues", "SubValues", 
  "SynthesizeMissingValues", "UpValues", "Values"}]

From the documentation for Select

Select[crit] represents an operator form of Select that can be applied
to an expression.

Notice that there is only one argument in the operator form. You can mimic the operator form using a pure function

Names["System`*Values"] // 
 Select[#, StringMatchQ[StartOfString ~~ "Down" | "Own" | "Sub" | "Up" ~~ "Values"], 2] &

Or you can generalize it

selectN[n_][predicate_] := Select[#, predicate, n] &

Names["System`*Values"] // 
 selectN[3][StringMatchQ[StartOfString ~~ "Down" | "Own" | "Sub" | "Up" ~~ "Values"]]

Or if you prefer

ClearAll@selectN
selectN[predicate_][n_] := Select[#, predicate, n] &

Names["System`*Values"] // 
 selectN[StringMatchQ[StartOfString ~~ "Down" | "Own" | "Sub" | "Up" ~~ "Values"]][3]

Taking the last method you suggest as an example, I want to let the following invoking forms of the customized function returns all matches:

Names["System`*Values"] // 
 selectN[StringMatchQ[
    StartOfString ~~ "Down" | "Own" | "Sub" | "Up" ~~ "Values"]][]

Names["System`*Values"] // 
 selectN[StringMatchQ[
   StartOfString ~~ "Down" | "Own" | "Sub" | "Up" ~~ "Values"]]

Is this possible?

It is possible for the first case

ClearAll@selectN
selectN[predicate_][n_ : Infinity] := Select[#, predicate, n] &

Names["System`*Values"] // 
 selectN[StringMatchQ[StartOfString ~~ "Down" | "Own" | "Sub" | "Up" ~~ "Values"]][]

Names["System`*Values"] // 
 selectN[StringMatchQ[StartOfString ~~ "Down" | "Own" | "Sub" | "Up" ~~ "Values"]][2]

You can use this to write select functions for different predicates e.g.

valuesSelect = 
  selectN[StringMatchQ[StartOfString ~~ "Down" | "Own" | "Sub" | "Up" ~~ "Values"]];
Names["System`*Values"] // valuesSelect[3]

unitDeterminantSelect = selectN[Det[#] == 1 &];
Tuples[{0, 1}, {3, 3}] // unitDeterminantSelect[3]

But if you need to be able to return all matches then maybe this is better

ClearAll@selectN
selectN[predicate_, n_ : Infinity] := Select[#, predicate, n] &

Names["System`*Values"] // 
 selectN[StringMatchQ[StartOfString ~~ "Down" | "Own" | "Sub" | "Up" ~~ "Values"]]

Names["System`*Values"] // 
 selectN[StringMatchQ[StartOfString ~~ "Down" | "Own" | "Sub" | "Up" ~~ "Values"], 2]

I tried the following command, but failed:

In[55]:= EntityList["FiniteGroup"] // 
 Select[StringMatchQ[RegularExpression[".*space.*"], 
   IgnoreCase -> True]]

During evaluation of In[55]:= StringMatchQ::strse: String or list of strings expected at position 1 in StringMatchQ[RegularExpression[.*space.*],IgnoreCase->True][alternating group of degree 1].

During evaluation of In[55]:= StringMatchQ::strse: String or list of strings expected at position 1 in StringMatchQ[RegularExpression[.*space.*],IgnoreCase->True][alternating group of degree 2].

During evaluation of In[55]:= StringMatchQ::strse: String or list of strings expected at position 1 in StringMatchQ[RegularExpression[.*space.*],IgnoreCase->True][crystallographic point group C1].

During evaluation of In[55]:= General::stop: Further output of StringMatchQ::strse will be suppressed during this calculation.

Out[55]= {}

There are several problems

  1. EntityList returns a List of Entity so CanonicalName needs to be used to get a String name.
  2. The CanonicalName is either a List where the first element is a String or just a String. To get the String in either case the two argument form of First has to be used.
  3. The operator form of StringMatchQ only takes one argument and no options, so a pure function has to be used

With those changes

EntityList["FiniteGroup"] // CanonicalName // 
 Select[StringMatchQ[First[#, #], RegularExpression[".*space.*"], IgnoreCase -> True] &]

the result is an empty list. To check that it is working correctly

EntityList["FiniteGroup"] // CanonicalName // 
 Select[StringMatchQ[First[#, #], RegularExpression[".*abelian.*"], IgnoreCase -> True] &]

I get the result as shown below with “.*quater.*” as the regex:

In[29]:= EntityList["FiniteGroup"] // CanonicalName // 
 Select[StringMatchQ[First[#, #], RegularExpression[".*quater.*"], 
    IgnoreCase -> True] &]

Out[29]= {"Quaternion"}

I want to obtain the corresponding result in a more meaningful manner, say, as shown below for this case:

{"DirectProduct", {"Quaternion", {"CyclicGroup", 2}}}