Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.6K Views
|
10 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Extract elements from a list using RegularExpression

Posted 3 years ago

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

POSTED BY: Hongyi Zhao
10 Replies
Posted 3 years ago

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]
POSTED BY: Rohit Namjoshi
Posted 3 years ago

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]
POSTED BY: Rohit Namjoshi
Posted 3 years ago

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}}}
POSTED BY: Hongyi Zhao
Posted 3 years ago
POSTED BY: Rohit Namjoshi
Posted 3 years ago

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]= {}
POSTED BY: Hongyi Zhao
Posted 3 years ago
POSTED BY: Hongyi Zhao
Posted 3 years ago

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"}]
POSTED BY: Hongyi Zhao
Posted 3 years ago

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]
POSTED BY: Updating Name
Posted 3 years ago

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.

POSTED BY: Hongyi Zhao
Posted 3 years ago

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]
POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard